VB.NET-扩展子类中的属性
也许有人可以帮我,我可以想象这是一个共同的需要: 我有一个基本类和一个子类。基类有一个名为“hello”的属性。现在我需要在子对象的属性集中添加扩展功能-我如何才能实现这一点 用于进一步解释的代码示例: 基类:VB.NET-扩展子类中的属性,vb.net,inheritance,properties,expand,Vb.net,Inheritance,Properties,Expand,也许有人可以帮我,我可以想象这是一个共同的需要: 我有一个基本类和一个子类。基类有一个名为“hello”的属性。现在我需要在子对象的属性集中添加扩展功能-我如何才能实现这一点 用于进一步解释的代码示例: 基类: Public MustInherit Class Base Private pHello as String = "" Public Property Hello As String Get Return pHello
Public MustInherit Class Base
Private pHello as String = ""
Public Property Hello As String
Get
Return pHello
End Get
Set(ByVal value As String)
pHello = value
'DoSomethingInBaseClass()
MsgBox "BaseClass calling!" 'Just for testing
End Set
End Property
End Class
儿童班
Public Class Child
Inherits Base
Public Property Hello As String
Get
Return MyBase.Hello
End Get
Set(ByVal value As String)
'DoSomethingInCHILDClass()
MsgBox "ChildClass calling!" 'Just for testing
End Set
End Property
End Class
Public MustInherit Class Base
Private pHello as String = ""
Public Property Hello As String
Get
Return pHello
End Get
Set(ByVal value As String)
pHello = value
doSomething()
MsgBox "BaseClass calling!" 'Just for testing
End Set
End Property
Private Overridable Sub doSomething()
'Do base class stuff
End Sub
End Class
Public Class Child
Inherits Base
Public Property Hello As String
Get
Return MyBase.Hello
End Get
Set(ByVal value As String)
doSomething()
MsgBox "ChildClass calling!" 'Just for testing
End Set
End Property
Private Overrides Sub doSomething()
'Do child class stuff
MyBase.doSomething()
End Sub
End Class
主目录中的属性集
Public Class Main
Public Sub DoIt()
Dim InstChild as new Child
InstChild.Hello = "test"
End Sub
End Class
基本上,我想要的是,在设置属性时,首先获取子MessageBox,然后获取基本MessageBox
确实,我需要在属性定义中添加关键字。
我玩过阴影和覆盖,但要么我只得到子消息,要么我只得到基本消息
有什么办法可以两者兼得吗
多谢各位 问题出在孩子班上。不要返回
myBase.hello
只需返回Me.hello
因为首先,子类的
me.hello
将等于基类的hello
。因此,当您重写属性时,基类上的属性将保持不变,并且只会在子类上更改。
因此,为了获得这两个属性,您应该调用:base.Hello.get()
和child.Hello.get()
我建议在可重写函数中执行此操作。通过这种方式,您可以让子类完成其工作,然后调用MyBase.OverridedFunction()
例如:
基类
Public Class Child
Inherits Base
Public Property Hello As String
Get
Return MyBase.Hello
End Get
Set(ByVal value As String)
'DoSomethingInCHILDClass()
MsgBox "ChildClass calling!" 'Just for testing
End Set
End Property
End Class
Public MustInherit Class Base
Private pHello as String = ""
Public Property Hello As String
Get
Return pHello
End Get
Set(ByVal value As String)
pHello = value
doSomething()
MsgBox "BaseClass calling!" 'Just for testing
End Set
End Property
Private Overridable Sub doSomething()
'Do base class stuff
End Sub
End Class
Public Class Child
Inherits Base
Public Property Hello As String
Get
Return MyBase.Hello
End Get
Set(ByVal value As String)
doSomething()
MsgBox "ChildClass calling!" 'Just for testing
End Set
End Property
Private Overrides Sub doSomething()
'Do child class stuff
MyBase.doSomething()
End Sub
End Class
儿童班
Public Class Child
Inherits Base
Public Property Hello As String
Get
Return MyBase.Hello
End Get
Set(ByVal value As String)
'DoSomethingInCHILDClass()
MsgBox "ChildClass calling!" 'Just for testing
End Set
End Property
End Class
Public MustInherit Class Base
Private pHello as String = ""
Public Property Hello As String
Get
Return pHello
End Get
Set(ByVal value As String)
pHello = value
doSomething()
MsgBox "BaseClass calling!" 'Just for testing
End Set
End Property
Private Overridable Sub doSomething()
'Do base class stuff
End Sub
End Class
Public Class Child
Inherits Base
Public Property Hello As String
Get
Return MyBase.Hello
End Get
Set(ByVal value As String)
doSomething()
MsgBox "ChildClass calling!" 'Just for testing
End Set
End Property
Private Overrides Sub doSomething()
'Do child class stuff
MyBase.doSomething()
End Sub
End Class
谢谢你的快速回复!我希望我对你的理解是正确的,但是把它改成我。在Child Get中的hello会导致递归调用。不管怎么说,我的问题在片场。首先调用子集合,然后调用基集合。不幸的是,我不能给你投票,我的代表太低了:(非常感谢!这很好!我以为会有一个更干净的解决方案,但至少这个解决方案在功能上是可行的。