定義類時,可以為數(shù)據(jù)類型定義藍圖。 這實際上并不定義任何數(shù)據(jù),但它定義了類名的含義,即類的對象將包含什么以及可以對這樣的對象執(zhí)行什么操作。
對象是類的實例。 構(gòu)成類的方法和變量稱為類的成員。
類的定義以關(guān)鍵字Class開頭,后跟類名稱; 和類體,由End Class語句結(jié)束。 以下是類定義的一般形式:
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _ Class name [ ( Of typelist ) ] [ Inherits classname ] [ Implements interfacenames ] [ statements ] End Class
attributelist 屬性列表: attributelist是一個適用于類的屬性列表。 可選的。
accessmodifier 訪問修改器:定義類的訪問級別,它的值為 - Public,Protected,F(xiàn)riend,Protected Friend和Private。 可選的。
Shadows 陰影: 陰影表示變量在基類中重新聲明和隱藏一個同名的元素或一組重載的元素。 可選的。
MustInherit:MustInherit指定該類只能用作基類,并且不能直接從它創(chuàng)建對象,即抽象類。 可選的。
NotInheritable 不可繼承: NotInheritable指定該類不能用作基類。
Partial 部分: Partial表示類的部分定義。
Inherits 繼承: Inherits指定它繼承的基類。
Implements 實現(xiàn):Implements指定類繼承的接口。
下面的示例演示了一個Box類,它有三個數(shù)據(jù)成員,長度,寬度和高度:
Module mybox Class Box Public length As Double ' Length of a box Public breadth As Double ' Breadth of a box Public height As Double ' Height of a box End Class Sub Main() Dim Box1 As Box = New Box() ' Declare Box1 of type Box Dim Box2 As Box = New Box() ' Declare Box2 of type Box Dim volume As Double = 0.0 ' Store the volume of a box here ' box 1 specification Box1.height = 5.0 Box1.length = 6.0 Box1.breadth = 7.0 ' box 2 specification Box2.height = 10.0 Box2.length = 12.0 Box2.breadth = 13.0 'volume of box 1 volume = Box1.height * Box1.length * Box1.breadth Console.WriteLine("Volume of Box1 : {0}", volume) 'volume of box 2 volume = Box2.height * Box2.length * Box2.breadth Console.WriteLine("Volume of Box2 : {0}", volume) Console.ReadKey() End Sub End Module
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Volume of Box1 : 210 Volume of Box2 : 1560
類的成員函數(shù)是一個函數(shù),它的定義或其原型在類定義中像任何其他變量一樣。 它對它所屬的類的任何對象進行操作,并且可以訪問該對象的類的所有成員。
成員變量是對象的屬性(從設(shè)計角度),它們被保持為私有以實現(xiàn)封裝。 這些變量只能使用public成員函數(shù)訪問。
讓我們把上面的概念設(shè)置并獲得類中不同類成員的值:
Module mybox Class Box Public length As Double ' Length of a box Public breadth As Double ' Breadth of a box Public height As Double ' Height of a box Public Sub setLength(ByVal len As Double) length = len End Sub Public Sub setBreadth(ByVal bre As Double) breadth = bre End Sub Public Sub setHeight(ByVal hei As Double) height = hei End Sub Public Function getVolume() As Double Return length * breadth * height End Function End Class Sub Main() Dim Box1 As Box = New Box() ' Declare Box1 of type Box Dim Box2 As Box = New Box() ' Declare Box2 of type Box Dim volume As Double = 0.0 ' Store the volume of a box here ' box 1 specification Box1.setLength(6.0) Box1.setBreadth(7.0) Box1.setHeight(5.0) 'box 2 specification Box2.setLength(12.0) Box2.setBreadth(13.0) Box2.setHeight(10.0) ' volume of box 1 volume = Box1.getVolume() Console.WriteLine("Volume of Box1 : {0}", volume) 'volume of box 2 volume = Box2.getVolume() Console.WriteLine("Volume of Box2 : {0}", volume) Console.ReadKey() End Sub End Module
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Volume of Box1 : 210 Volume of Box2 : 1560
類構(gòu)造函數(shù)是每當(dāng)我們創(chuàng)建該類的新對象時執(zhí)行的類的特殊成員子類。 構(gòu)造函數(shù)具有名稱New,并且沒有任何返回類型。
下面的程序解釋了構(gòu)造函數(shù)的概念:
Class Line Private length As Double ' Length of a line Public Sub New() 'constructor Console.WriteLine("Object is being created") End Sub Public Sub setLength(ByVal len As Double) length = len End Sub Public Function getLength() As Double Return length End Function Shared Sub Main() Dim line As Line = New Line() 'set line length line.setLength(6.0) Console.WriteLine("Length of line : {0}", line.getLength()) Console.ReadKey() End Sub End Class
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Object is being created
Length of line : 6
默認構(gòu)造函數(shù)沒有任何參數(shù),但如果需要,構(gòu)造函數(shù)可以有參數(shù)。 這樣的構(gòu)造函數(shù)稱為參數(shù)化構(gòu)造函數(shù)。 此技術(shù)可幫助您在創(chuàng)建對象時為其分配初始值,如以下示例所示:
Class Line Private length As Double ' Length of a line Public Sub New(ByVal len As Double) 'parameterised constructor Console.WriteLine("Object is being created, length = {0}", len) length = len End Sub Public Sub setLength(ByVal len As Double) length = len End Sub Public Function getLength() As Double Return length End Function Shared Sub Main() Dim line As Line = New Line(10.0) Console.WriteLine("Length of line set by constructor : {0}", line.getLength()) 'set line length line.setLength(6.0) Console.WriteLine("Length of line set by setLength : {0}", line.getLength()) Console.ReadKey() End Sub End Class
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Object is being created, length = 10 Length of line set by constructor : 10 Length of line set by setLength : 6
析構(gòu)函數(shù)是一個類的特殊成員Sub,只要它的類的對象超出范圍,它就被執(zhí)行。
析構(gòu)函數(shù)名為Finalize,它既不能返回值也不能接受任何參數(shù)。 析構(gòu)函數(shù)在釋放程序之前釋放資源非常有用,比如關(guān)閉文件,釋放內(nèi)存等。
析構(gòu)函數(shù)不能繼承或重載。
下面的例子解釋析構(gòu)函數(shù)的概念:
Class Line Private length As Double ' Length of a line Public Sub New() 'parameterised constructor Console.WriteLine("Object is being created") End Sub Protected Overrides Sub Finalize() ' destructor Console.WriteLine("Object is being deleted") End Sub Public Sub setLength(ByVal len As Double) length = len End Sub Public Function getLength() As Double Return length End Function Shared Sub Main() Dim line As Line = New Line() 'set line length line.setLength(6.0) Console.WriteLine("Length of line : {0}", line.getLength()) Console.ReadKey() End Sub End Class
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Object is being created Length of line : 6 Object is being deleted
我們可以使用Shared關(guān)鍵字將類成員定義為靜態(tài)。 當(dāng)我們將一個類的成員聲明為Shared時,意味著無論創(chuàng)建多少個對象,該成員只有一個副本。
關(guān)鍵字“共享”意味著類只存在一個成員實例。 共享變量用于定義常量,因為它們的值可以通過調(diào)用類而不創(chuàng)建它的實例來檢索。
共享變量可以在成員函數(shù)或類定義之外初始化。 您還可以在類定義中初始化共享變量。
您還可以將成員函數(shù)聲明為共享。 這樣的函數(shù)只能訪問共享變量。 共享函數(shù)甚至在創(chuàng)建對象之前就存在。
以下示例演示了共享成員的使用:
Class StaticVar Public Shared num As Integer Public Sub count() num = num + 1 End Sub Public Shared Function getNum() As Integer Return num End Function Shared Sub Main() Dim s As StaticVar = New StaticVar() s.count() s.count() s.count() Console.WriteLine("Value of variable num: {0}", StaticVar.getNum()) Console.ReadKey() End Sub End Class
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Value of variable num: 3
面向?qū)ο缶幊讨凶钪匾母拍钪皇抢^承。 繼承允許我們根據(jù)另一個類來定義一個類,這使得更容易創(chuàng)建和維護應(yīng)用程序。 這也提供了重用代碼功能和快速實現(xiàn)時間的機會。
在創(chuàng)建類時,程序員可以指定新類應(yīng)該繼承現(xiàn)有類的成員,而不是編寫完全新的數(shù)據(jù)成員和成員函數(shù)。 這個現(xiàn)有類稱為基類,新類稱為派生類。
類可以從多個類或接口派生,這意味著它可以從多個基類或接口繼承數(shù)據(jù)和函數(shù)。
VB.Net中用于創(chuàng)建派生類的語法如下:
<access-specifier> Class <base_class> ... End Class Class <derived_class>: Inherits <base_class> ... End Class
考慮一個基類Shape和它的派生類Rectangle:
' Base class Class Shape Protected width As Integer Protected height As Integer Public Sub setWidth(ByVal w As Integer) width = w End Sub Public Sub setHeight(ByVal h As Integer) height = h End Sub End Class ' Derived class Class Rectangle : Inherits Shape Public Function getArea() As Integer Return (width * height) End Function End Class Class RectangleTester Shared Sub Main() Dim rect As Rectangle = New Rectangle() rect.setWidth(5) rect.setHeight(7) ' Print the area of the object. Console.WriteLine("Total area: {0}", rect.getArea()) Console.ReadKey() End Sub End Class
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Total area: 35
派生類繼承基類成員變量和成員方法。 因此,應(yīng)該在創(chuàng)建子類之前創(chuàng)建超類對象。 超類或基類在VB.Net中被稱為MyBase
以下程序演示了這一點:
' Base class Class Rectangle Protected width As Double Protected length As Double Public Sub New(ByVal l As Double, ByVal w As Double) length = l width = w End Sub Public Function GetArea() As Double Return (width * length) End Function Public Overridable Sub Display() Console.WriteLine("Length: {0}", length) Console.WriteLine("Width: {0}", width) Console.WriteLine("Area: {0}", GetArea()) End Sub 'end class Rectangle End Class 'Derived class Class Tabletop : Inherits Rectangle Private cost As Double Public Sub New(ByVal l As Double, ByVal w As Double) MyBase.New(l, w) End Sub Public Function GetCost() As Double Dim cost As Double cost = GetArea() * 70 Return cost End Function Public Overrides Sub Display() MyBase.Display() Console.WriteLine("Cost: {0}", GetCost()) End Sub 'end class Tabletop End Class Class RectangleTester Shared Sub Main() Dim t As Tabletop = New Tabletop(4.5, 7.5) t.Display() Console.ReadKey() End Sub End Class
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5
VB.Net支持多重繼承。
更多建議: