VB.NET - 扩展子类中的属性

时间:2015-05-22 09:28:17

标签: vb.net inheritance properties expand

也许有人可以帮助我,我可以想象这是一个共同的需求: 我有一个基地和一个儿童班。基类有一个名为“hello”的属性。现在我需要在子属性Set中添加扩展功能 - 我该如何实现?

代码示例以获得进一步说明:

基类:

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 Class Main

    Public Sub DoIt()
        Dim InstChild as new Child
        InstChild.Hello = "test"
    End Sub

End Class

基本上我想要的是,在设置属性时,我首先得到Child MessageBox,然后是Base MessageBox。

当然我需要在Property定义中添加一个关键字。 我玩过Shadows和Overrides,但要么我只得到Child,要么只得到Base Message。

有没有办法让两者兼得?

非常感谢!

2 个答案:

答案 0 :(得分:1)

我建议在可覆盖的功能中完成工作。这样,您可以让子类执行其工作,然后调用MyBase.overriddenFunction()。

例如:

基类

//override initWithStyle for custom cell
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

//if initialized successfully, create ui
if(self)
{
    //get the cell's frame size
    CGSize cellSize = self.contentView.frame.size;
    //CGSize viewSize = self.frame.size;
    self.backgroundColor = [UIColor blueColor];

    //set up the player number label
    self.playerNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(8.0, 4.0, (cellSize.width/2 - 16), (cellSize.height - 8))];
    [self.playerNumberLabel setFont:[UIFont fontWithName:@"Aka-AcidGR-ScrachThis" size:30]];
    [self.playerNumberLabel setTextColor:[UIColor whiteColor]];
    [self.playerNumberLabel setTextAlignment:NSTextAlignmentLeft];
    [self.playerNumberLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

    //add label to cell
    [self.contentView addSubview:self.playerNumberLabel];

    //set up the textfield for the cell
    self.playerNameTextField = [[UITextField alloc] initWithFrame:CGRectMake( cellSize.width/2, 4.0, (cellSize.width/2 - 8), (cellSize.height - 8))];
    [self.playerNameTextField setBackgroundColor:[UIColor whiteColor]];
    [self.playerNameTextField setBorderStyle:UITextBorderStyleRoundedRect];

    [self.contentView addSubview:self.playerNameTextField];

}

return self;
}

儿童班

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

答案 1 :(得分:0)

问题出在儿童班。而不是返回myBase.hello只返回Me.hello
因为子类的第一个me.hello将等于hello的{​​{1}}。因此,当您覆盖该属性时,它将保持相同,并且只会在子类上更改。
所以为了让你们两个都应该调用:base class和{{ 1}}