继承并向子类添加属性

时间:2014-09-10 13:27:39

标签: .net vb.net class oop inheritance

我可以从一个类继承,并为我的新类添加属性......这一切都很好。

Class class1

 Property a as string

End Class

Class class2
 inherits class1

 property b as string

End Class

Dim mytest as new class2

mytest.a = "bleh"
mytest.b = "bah"

如何在我继承的类中为子类添加属性?

Class class3

 Property a as string

 Class class3_1
   Property c as string
 End Class

End Class

如何创建一个新的类来加入class3,但是在我新的enherited类中添加一个属性给class3_1?

使用实际非工作代码进行新编辑

Public Class testclass

    Public Class class1

        Property x As String
        Property firstChildClass As New class1_1

        Public Class class1_1
            Property a As String
        End Class

    End Class

    Public Class inherited_class1
        Inherits class1

        Public Class class1_1  '///// Warning here to use "Shadows" keyword.. but that also does not work... takes away the warning, but still cant access property b when using a new instance of class
            Property b As String
        End Class

    End Class

    Sub test()

        Dim myTestClass As New inherited_class1

        myTestClass.firstChildClass.b = "blah" '///// This does not work...

    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

只需将包含类包含在子类的继承中。您必须单独继承包含类和子类:

Public Class inherited_class1
    Inherits class1

    Public Class class1_2
        Inherits class1_1
        Public Property b() As String
    End Class

End Class


Public Sub test()
    Dim myTestClass As New inherited_class1()

    Dim ic As New inherited_class1.class1_2()
    myTestClass.firstChildClass = ic
    ic.b = "blah"
    '''//// This does not work...
End Sub