Vb.net继承问题 [英] Vb.net inheritance question
问题描述
我有一个关于继承的问题。
我有很多课程在运行时动态地反映到一个更大的项目中。所有类都需要有一组标准的方法和属性来与这个更大的项目进行交互,并且很多开销代码在类之间是相似的,所以我有一个基类来处理属性和方法,然后是一个子类,通过继承来处理每个块的特定代码。
这是问题所在:
在基类中,有一个叫做的属性Memory声明为Object。这在子类中用于存储数据,它需要能够包含任何类型的数据,因此我有一个名为InternalMemory的类,Memory属性被转换为。对于我的所有子类,这需要以相同的方式发生,因此我想在基类中处理它,但是它转换为的类型需要在子类中定义。有没有办法做到这一点?
这里有一些简化的示例代码。
Public MustInherit Class BlockOverhead
Public Property Memory As Object
Get
Return BlockMemory
End Get
Set(value As Object)
BlockMemory = value
结束集
结束属性
受保护的BlockMemory为新对象
Public Sub TryExecute()
'============ ================================================== ============
'尝试导入块内存
'====================== ================================================== ==
InternalBlockMem = TryCast(BlockMemory,Object.GetType(InternalBlockMem))
If IsNothing(InternalBlockMem)Then
InternalBlockMem = New InternalMemory
End if
'================================================ ================= =========
'尝试执行块
'========================== ================================================
执行()
'==================================== ======================================
'保存块内存
'================================================ ==========================
BlockMemory =新对象
BlockMemory = InternalBlockMem
结束Sub
受保护的MustOverride子执行()
结束类
公共类Magic1
继承BlockOverhead
公共类InternalMemory
'创建块的内存变量这里。
Public OutValue1 As Integer
Public OutValue2 As Boolean
End Class
受保护的阴影InternalBlockMem As New InternalMemory
受保护的覆盖Sub Execute()
'做一些东西给InternalblockMemory
InternalBlockMem.OutValue1 = 0
InternalBlockMem.OutValue2 = True
结束Sub
结束类
公共类Magic2
继承BlockOverhead
公共类InternalMemory
'在此创建块的内存变量。
Public Test As New List(Of String)
Public Value As Decimal
End Class
受保护的阴影InternalBlockMem As New InternalMemory
受保护的覆盖Sub Execute( )
'对InternalblockMemory做一些事情
InternalBlockMem.Test.Clear()
InternalBlockMem.Test.Add(Stuff)
InternalBlockMem.Value = InternalBlockMem.Value + 1
结束次级
结束等级
我尝试过:
我尝试了阴影,但基类中的格式化修改了变量的基类实例。
尝试谷歌几个小时。也没有运气。
听起来你正在寻找泛型:
Visual Basic中的常规类型(Visual Basic)|英特尔®开发人员专区Microsoft Docs [ ^ ]
公共 < span class =code-keyword> MustInherit 类 BlockOverhead( TMemory 作为 {类,新})
公开 属性内存为 TMemory = 新 TMemory
公开 Sub TryExecute()
Dim myMemory As TMemory = Memory
如果 myMemoryNothing 然后
myMemory = 新 TMemory
结束 如果
执行(myMemory)
内存= myMemory
结束 Sub
受保护的 MustOverride Sub 执行( ByVal memory As TMemory)
结束 类
公共 类 Magic1
继承 BlockOverhead(的 InternalMemory)
公开 类 InternalMemory
公开 OutValue1 作为 整数
公众 OutValue2 作为 布尔
结束 类
受保护的 覆盖 Sub 执行( ByVal memory As InternalMemory)
memory.OutValue1 = 42
memory.OutValue2 = True
结束 Sub
结束 类跨度>
I've got a question about inheritance.
I've got a bunch of classes that get reflected into a larger project dynammically at run time. All the classes need to have a standard set of methods and properties to interact with this larger project and a lot of overhead code is similar between the classes, so I have a base class that handles the properties and methods, and then a child class that takes care of the specific code for each block via inheritance.
Here's the problem:
In the base class, there is a property called "Memory" declared as an Object. This is used inside the child class to store data, and it needs to be able to contain any type of data, so I have a class called "InternalMemory" which that Memory property gets converted to. This needs to happen the same way for all my child classes, so I'd like to handle it in the base class, but the type it gets converted to needs to be defined in the child class. Is there any way to do this?
Here's some simplified example code.
Public MustInherit Class BlockOverhead Public Property Memory As Object Get Return BlockMemory End Get Set(value As Object) BlockMemory = value End Set End Property Protected BlockMemory As New Object Public Sub TryExecute() '========================================================================== 'Try to import the block memory '========================================================================== InternalBlockMem = TryCast(BlockMemory, Object.GetType(InternalBlockMem)) If IsNothing(InternalBlockMem) Then InternalBlockMem = New InternalMemory End If '========================================================================== 'Try to execute the block '========================================================================== Execute() '========================================================================== 'Save the block memory '========================================================================== BlockMemory = New Object BlockMemory = InternalBlockMem End Sub Protected MustOverride Sub Execute() End Class Public Class Magic1 Inherits BlockOverhead Public Class InternalMemory 'Create block's memory vars here. Public OutValue1 As Integer Public OutValue2 As Boolean End Class Protected Shadows InternalBlockMem As New InternalMemory Protected Overrides Sub Execute() 'Do some stuff to InternalblockMemory InternalBlockMem.OutValue1 = 0 InternalBlockMem.OutValue2 = True End Sub End Class Public Class Magic2 Inherits BlockOverhead Public Class InternalMemory 'Create block's memory vars here. Public Test As New List(Of String) Public Value As Decimal End Class Protected Shadows InternalBlockMem As New InternalMemory Protected Overrides Sub Execute() 'Do some stuff to InternalblockMemory InternalBlockMem.Test.Clear() InternalBlockMem.Test.Add("Stuff") InternalBlockMem.Value = InternalBlockMem.Value + 1 End Sub End Class
What I have tried:
I've tried shadowing, but formatting in the base class modified the base class instance of the variable.
Tried google for several hours. No luck there either.
Sounds like you're looking for generics:
Generic Types in Visual Basic (Visual Basic) | Microsoft Docs[^]
Public MustInherit Class BlockOverhead(Of TMemory As {Class, New}) Public Property Memory As TMemory = New TMemory Public Sub TryExecute() Dim myMemory As TMemory = Memory If myMemory Is Nothing Then myMemory = New TMemory End If Execute(myMemory) Memory = myMemory End Sub Protected MustOverride Sub Execute(ByVal memory As TMemory) End Class Public Class Magic1 Inherits BlockOverhead(Of InternalMemory) Public Class InternalMemory Public OutValue1 As Integer Public OutValue2 As Boolean End Class Protected Overrides Sub Execute(ByVal memory As InternalMemory) memory.OutValue1 = 42 memory.OutValue2 = True End Sub End Class
这篇关于Vb.net继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!