用继承建立一个继承类2007-05-24 本站 "Inherits"关键字可以用在使一个类继承另一个类的属性、方法、事件等等,所有的类缺省的都是可以继承的,除非被设置为"NoInheritable"关键字。
下面这个例子定义了两个类,第一个类是一个基础类,并且含有一个属性和两个方法,第二个类从第一个类继承了这个属性和两个方法,重载了第二个方法,并且定义了一个新的属性"intProp2"。
Class Class1Private intProp1 as IntegerSub Method1() Messagebox.Show("这是在基本类中的一个方法")End SubOverridable Sub aMethod() Messagebox.Show("这是在基本类中的另一个方法")End SubProperty Prop1 As Integer GetProp1=intProp1 End Get SetintProp1=Value End SetEnd PropertyEnd ClassClass Class2Private intProp2 As IntegerInherits Class1property Prop2 As IntegerGetProp2=intProp2End GetSetIntProp2=ValueEnd SetEnd PropertyOverrides Sub aMethod()Messagebox.Show("这是在继承类中的一个方法")End SubEnd ClassProtected Sub TestInheritance()Dim C1 As New class1()Dim C2 As New class2()C1.Method1()C2.aMethod()C2.Method1()End Sub
当用户运行过程TextInheritance以后,可以看见如下的信息:
“这是在基本类中的一个方法”
“这是在基本类中的另一个方法”
“这是在基本类中的一个方法”
“这是在继承类中的一个方法”