本文介绍了Vb.net继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个关于继承的问题。 我有很多课程在运行时动态地反映到一个更大的项目中。所有类都需要有一组标准的方法和属性来与这个更大的项目进行交互,并且很多开销代码在类之间是相似的,所以我有一个基类来处理属性和方法,然后是一个子类,通过继承来处理每个块的特定代码。 这是问题所在: 在基类中,有一个叫做的属性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 结束次级结束等级 我尝试过: 我尝试了阴影,但基类中的格式化修改了变量的基类实例。 尝试谷歌几个小时。也没有运气。解决方案 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 ClassPublic 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 SubEnd ClassPublic 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 SubEnd ClassWhat 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. 解决方案 这篇关于Vb.net继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 21:04