抽象类,继承,覆盖等。 [英] Abstract classes, inherit, overwrite etc.

查看:101
本文介绍了抽象类,继承,覆盖等。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,

我想定义一个(超级)类来隐藏几个(子)类的代码,这些类都提供相同的方法,但代码不同。



简化示例:

在应用程序Form1中,我使用参数PrinterType(枚举)实例化(超级)类Printer。这个Printer类提供了Print()方法。在不同硬件上打印的代码应该在不同的类中编码。因此Printer类必须使用/继承其中一个特定(子)类。



我找到的唯一方法是声明Print()方法(子)类共享。但这对我来说是在公共模块中定义一个Sub()。

我确信有更好的编程方式作为我的解决方法。



我尝试过:



Hi Experts,
I want to define a (super)class that hides the code of several (sub)classes, which all provides the same methods, but with different code.

Simplified example:
In an application "Form1" I instance the (Super)class "Printer" with the argument "PrinterType" (enum). This "Printer" class provides the "Print()" method. The code for printing on different hardware should be coded in separate classes. So the "Printer" class has to use/inherit one of the specific (sub)classes.

The only way I found is to declare the Print() method in the (sub)classes as shared. But this is for me as defining a Sub() in a public module.
I'm sure there is a better way of programming as my workaround.

What I have tried:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim oPrinter As New Printer(Printer.PrinterType.HP)
        oPrinter.Print()
    End Sub
End Class


Public Class Printer

    Public Enum PrinterType
        HP
        Epson
    End Enum

    Public ActivePrinter As PrinterType

    Public Sub New(ByVal mPrinterType As PrinterType)
        ActivePrinter = mPrinterType
    End Sub

    Public Sub Print()
        Select Case ActivePrinter
            Case PrinterType.HP
                HP_Printer.Print()
            Case PrinterType.Epson
                Epson_Printer.Print()
        End Select
    End Sub

End Class


Public Class HP_Printer
    Public Shared Sub Print()
        Beep()
    End Sub
End Class


Public Class Epson_Printer
    Public Shared Sub Print()
        Beep()
    End Sub
End Class

推荐答案

如果我正确理解了这个问题,你正在寻找继承语句 [ ^ ]



您可能觉得有用的教程很少

- Microsoft .NET中基类的继承 [ ^ ]

- 继承基础知识(Visual Basic) [ ^ ]

- VB.Net - Classes&对象 [ ^ ]



[已添加]

根据你在解决方案3中的评论,我不确定你是否经过多次继承。如果是这种情况,则不支持.Net



我不确定你是否真的是指仿制药。这将帮助您在仍然使用不同类操作时具有通用功能。看一下 Visual Basic中的通用类型(Visual Basic) [ ^ ]
If I understand the question correctly, you're looking for Inherits Statement[^]

Few tutorials you might find useful
- Inheritance from a Base Class in Microsoft .NET[^]
- Inheritance Basics (Visual Basic)[^]
- VB.Net - Classes & Objects[^]

[ADDED]
Based on your comment in Solution 3 I'm not sure if you're after multiple inheritance. If that is the case, then it's not supported in .Net

I'm not sure if you actually mean generics. This would help you to have common functionality while still operating with different classes. Have a look at Generic Types in Visual Basic (Visual Basic)[^]


导出你的从您的基础打印机类中分类HP_Printer和Epson_Printer,它们可以覆盖基类实现以提供特定于模型的打印功能。



Microsoft .NET中基类的继承 [ ^ ]和继承基础知识(Visual Basic) [ ^ ]可能有帮助。
Derive your classes HP_Printer and Epson_Printer from your base Printer class, and they can override the base class implementation to provide model-specific Print functions.

Inheritance from a Base Class in Microsoft .NET[^] and Inheritance Basics (Visual Basic)[^] may help.


亲爱的专家,



Richard

of co这只是一个容易理解的例子,我的应用程序中的问题与Windows打印机无关。



Mika,OriginalGriff

我我想我理解了Inherits语句,这意味着我创建了一个使用base(parent)类代码的新类。但是要实例化这个新类,我必须创建一个具有新类类型的对象。我需要相反的方式。

我寻找一种方法,基类可以根据NEW()方法的参数继承不同类中的方法。



请尝试以这种方式纠正我的代码行。
Dear Experts,

Richard
of course this is only a easy understandable example, the problem in my application has nothing to do with Windows printers.

Mika, OriginalGriff
I think I understand the Inherits statement, it means I create a new class which uses code of the base (parent) class. But to instance this new class I have to create an object with the type of the new class. I need the opposite way.
I look for a way that the base class can inherit methods from different classes depending on an argument of the NEW() method.

Please try to correct my lines of code in that way.


这篇关于抽象类,继承,覆盖等。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆