
- VB.Net 基礎教程
- VB.Net - 首頁
- VB.Net - 概述
- VB.Net - 環境設定
- VB.Net - 程式結構
- VB.Net - 基本語法
- VB.Net - 資料型別
- VB.Net - 變數
- VB.Net - 常量
- VB.Net - 修飾符
- VB.Net - 語句
- VB.Net - 指令
- VB.Net - 運算子
- VB.Net - 決策
- VB.Net - 迴圈
- VB.Net - 字串
- VB.Net - 日期與時間
- VB.Net - 陣列
- VB.Net - 集合
- VB.Net - 函式
- VB.Net - 子程式
- VB.Net - 類與物件
- VB.Net - 異常處理
- VB.Net - 檔案處理
- VB.Net - 基本控制元件
- VB.Net - 對話方塊
- VB.Net - 高階窗體
- VB.Net - 事件處理
- VB.Net 高階教程
- VB.Net - 正則表示式
- VB.Net - 資料庫訪問
- VB.Net - Excel 表格
- VB.Net - 傳送郵件
- VB.Net - XML 處理
- VB.Net - Web 程式設計
- VB.Net 有用資源
- VB.Net - 快速指南
- VB.Net - 有用資源
- VB.Net - 討論
VB.Net - 類與物件
當您定義一個類時,您定義了一個數據型別的藍圖。這實際上並沒有定義任何資料,但它確實定義了類名的含義,也就是說,類的物件將包含什麼以及對這樣的物件可以執行哪些操作。
物件是類的例項。構成類的那些方法和變數稱為類的成員。
類定義
類定義以關鍵字Class開頭,後跟類名;以及類體,以End Class語句結束。以下是類定義的一般形式:
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _ Class name [ ( Of typelist ) ] [ Inherits classname ] [ Implements interfacenames ] [ statements ] End Class
其中,
attributelist 是應用於類的屬性列表。可選。
accessmodifier 定義類的訪問級別,其值包括 - Public、Protected、Friend、Protected Friend 和 Private。可選。
Shadows 指示變數重新宣告並隱藏基類中同名元素或一組過載元素。可選。
MustInherit 指定該類只能用作基類,並且不能直接從中建立物件,即抽象類。可選。
NotInheritable 指定該類不能用作基類。
Partial 指示類的部分定義。
Inherits 指定它繼承的基類。
Implements 指定類繼承的介面。
以下示例演示了一個Box類,它具有三個資料成員,長度、寬度和高度:
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
當以上程式碼編譯並執行時,會產生以下結果:
Volume of Box1 : 210 Volume of Box2 : 1560
成員函式和封裝
類的成員函式是在類定義中定義或其原型與任何其他變數一樣的函式。它對它是成員的類的任何物件進行操作,並且可以訪問該物件類的所有成員。
成員變數是物件(從設計角度來看)的屬性,並且為了實現封裝而被設定為私有。這些變數只能使用公共成員函式訪問。
讓我們將上述概念應用於在類中設定和獲取不同類成員的值:
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
當以上程式碼編譯並執行時,會產生以下結果:
Volume of Box1 : 210 Volume of Box2 : 1560
建構函式和解構函式
類的建構函式是類的一個特殊的成員Sub,每當我們建立該類的新的物件時都會執行它。建構函式的名稱為New,它沒有任何返回型別。
以下程式解釋了建構函式的概念:
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
當以上程式碼編譯並執行時,會產生以下結果:
Object is being created Length of line : 6
預設建構函式沒有任何引數,但如果需要,建構函式可以有引數。這樣的建構函式稱為引數化建構函式。此技術有助於在建立物件時為其分配初始值,如下例所示:
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
當以上程式碼編譯並執行時,會產生以下結果:
Object is being created, length = 10 Length of line set by constructor : 10 Length of line set by setLength : 6
解構函式是類的一個特殊的成員Sub,每當其類的物件超出範圍時都會執行它。
解構函式的名稱為Finalize,它既不能返回值也不能接受任何引數。解構函式在退出程式之前釋放資源(例如關閉檔案、釋放記憶體等)時非常有用。
解構函式不能被繼承或過載。
以下示例解釋了解構函式的概念:
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
當以上程式碼編譯並執行時,會產生以下結果:
Object is being created Length of line : 6 Object is being deleted
VB.Net 類的共享成員
我們可以使用Shared關鍵字將類成員定義為靜態。當我們將類的成員宣告為Shared時,這意味著無論建立了多少個類的物件,該成員都只有一個副本。
關鍵字Shared意味著類只有一個成員例項。共享變數用於定義常量,因為可以透過呼叫類來檢索它們的值,而無需建立類的例項。
共享變數可以在成員函式或類定義之外初始化。您也可以在類定義內初始化共享變數。
您還可以將成員函式宣告為Shared。此類函式只能訪問共享變數。共享函式在物件建立之前就已存在。
以下示例演示了共享成員的使用:
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
當以上程式碼編譯並執行時,會產生以下結果:
Value of variable num: 3
繼承
面向物件程式設計中最重要的概念之一是繼承。繼承允許我們根據另一個類來定義一個類,這使得建立和維護應用程式變得更容易。這也提供了重用程式碼功能和快速實現時間的機會。
在建立類時,程式設計師可以指定新類應該繼承現有類的成員,而不是完全編寫新的資料成員和成員函式。這個現有的類稱為基類,新類稱為派生類。
基類和派生類
一個類可以從多個類或介面派生,這意味著它可以繼承多個基類或介面的資料和函式。
VB.Net 中用於建立派生類的語法如下:
<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
當以上程式碼編譯並執行時,會產生以下結果:
Total area: 35
基類初始化
派生類繼承基類成員變數和成員方法。因此,在建立子類之前,應該先建立超類物件。超類或基類在 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
當以上程式碼編譯並執行時,會產生以下結果:
Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5
VB.Net 支援多重繼承。