"Inherits"關鍵字可以用在使一個類繼承另一個類的屬性、方法、事件等等,所有的類缺省的都是可以繼承的,除非被設置為"NoInheritable"關鍵字。
下面這個例子定義了兩個類,第一個類是一個基礎類,並且含有一個屬性和兩個方法,第二個類從第一個類繼承了這個屬性和兩個方法,重載了第二個方法,並且定義了一個新的屬性"intProp2"。
Class Class1
Private intProp1 as Integer
Sub Method1()
Messagebox.Show("這是在基本類中的一個方法")
End Sub
Overridable Sub aMethod()
Messagebox.Show("這是在基本類中的另一個方法")
End Sub
Property Prop1 As Integer
Get
Prop1=intProp1
End Get
Set
intProp1=Value
End Set
End Property
End Class
Class Class2
Private intProp2 As Integer
Inherits Class1
property Prop2 As Integer
Get
Prop2=intProp2
End Get
Set
IntProp2=Value
End Set
End Property
Overrides Sub aMethod()
Messagebox.Show("這是在繼承類中的一個方法")
End Sub
End Class
Protected Sub TestInheritance()
Dim C1 As New class1()
Dim C2 As New class2()
C1.Method1()
C2.aMethod()
C2.Method1()
End Sub
當用戶運行過程TextInheritance以後,可以看見如下的信息:
“這是在基本類中的一個方法”
“這是在基本類中的另一個方法”
“這是在基本類中的一個方法”
“這是在繼承類中的一個方法”