VB.NET (Visual Basic 7) 是完整的物件導向語言,支援封裝、繼承、多型、介面四大支柱,並提供命名空間、屬性、事件、泛型、委派等現代語言特性。對於 VB6 開發者,核心思維轉變在於:「從表單/模組中心」轉向「類別/物件中心」。
Public Class Employee
' 類別內容
End Class
Class / End Class。GeneralManager、StringUtil。| 類型 | 關鍵字 | 命名慣例 | 說明 |
|---|---|---|---|
| 欄位 | Dim / Private / Public | camelCase | 儲存狀態,如 salary、yearlyBonus |
| 屬性 | Property | PascalCase | 封裝欄位存取,可含驗證邏輯 |
| 方法 | Sub / Function | PascalCase | 行為,如 Work()、CalculateSalary() |
| 事件 | Event | PascalCase | 通知機制,配合 RaiseEvent/Handles |
| 建構函式 | Sub New | — | 物件初始化,可多載 |
| 解構/釋放 | Finalize / IDisposable | — | 非受控資源清理 |
Imports System
Public Class Employee
Private _salary As Decimal = 40000D
Private _yearlyBonus As Decimal = 4000D
Public Sub New() {} ' 無參數建構子
Public Sub New(salary As Decimal) ' 多載建構子
_salary = salary
End Sub
Public ReadOnly Property Salary As Decimal
Get
Return _salary
End Get
End Property
Public Sub PrintSalary()
Console.WriteLine("薪資: {0:C}", _salary)
End Sub
Public Shared Sub Main() ' 程式進入點
Dim emp As New Employee(50000D)
emp.PrintSalary()
End Sub
End Class
Imports System ' 匯入命名空間,後續可直接用 Console.WriteLine
Imports MyCompany.HR ' 自訂命名空間
Namespace MyCompany.HR
Public Class Employee
' ...
End Class
End Namespace
Imports 簡化程式碼,但同名類別需用完整名稱區分(如 System.Console vs MyApp.Console)。System、System.IO、System.Collections.Generic、System.Linq 等。| 修飾詞 | 可存取範圍 | 適用對象 |
|---|---|---|
Public | 任何地方 | 公開 API、介面 |
Private | 僅本類別內部 | 內部實作細節、欄位 |
Protected | 本類別 + 衍生類別 | 供子類別擴充的成員 |
Friend (C#: internal) | 同一組件內部 | 組件層級內部 API |
Protected Friend | 衍生類別 + 同組件 | 罕見,特定架構需求 |
原則:預設 Private,按需最小化公開介面,實現資訊隱藏。
Public Class SalaryLevel
Public Shared Level1 As Decimal = 35000D
Public Shared Level2 As Decimal = 40000D
Public Shared Level3 As Decimal = 45000D
Public Shared Function GetLevelName(level As Decimal) As String
Select Case level
Case Level1 : Return "初級"
Case Level2 : Return "中級"
Case Level3 : Return "高級"
Case Else : Return "未知"
End Select
End Function
End Class
' 使用方式
Console.WriteLine(SalaryLevel.Level2) ' 40000
Console.WriteLine(SalaryLevel.GetLevelName(40000)) ' 中級
Shared (C#: static) 成員屬於類別而非實例。Me 參考)。Public MustInherit Class Shape ' 抽象基類
Public MustOverride Function Area() As Double
End Class
Public Class Circle : Inherits Shape
Public Property Radius As Double
Public Overrides Function Area() As Double
Return Math.PI * Radius * Radius
End Function
End Class
Public Class Rectangle : Inherits Shape
Public Property Width, Height As Double
Public Overrides Function Area() As Double
Return Width * Height
End Function
End Class
' 多型使用
Dim shapes As New List(Of Shape) From {New Circle With {.Radius = 2}, New Rectangle With {.Width = 3, .Height = 4}}
For Each s In shapes : Console.WriteLine(s.Area()) : Next
Inherits 繼承單一基類,Implements 實作多個介面。MustInherit/MustOverride 定義抽象類別/方法。Overridable/Overrides 實現執行期多型。NotOverridable (C#: sealed) 禁止進一步覆寫。Public Interface ISerializable
Sub Save(writer As IO.TextWriter)
Sub Load(reader As IO.TextReader)
End Interface
Public Class Employee : Implements ISerializable
Public Sub Save(writer As IO.TextWriter) Implements ISerializable.Save
' 實作儲存邏輯
End Sub
Public Sub Load(reader As IO.TextReader) Implements ISerializable.Load
' 實作載入邏輯
End Sub
End Class
Public Class Person
Public Property Name As String
Public Property Age As Integer
Public Sub New(name As String)
Me.New(name, 0) ' 呼叫另一個建構子
End Sub
Public Sub New(name As String, age As Integer)
Me.Name = name : Me.Age = age
End Sub
End Class
' 物件初始化器 (VB 9+ / VS 2008+)
Dim p As New Person With {.Name = "Tom", .Age = 25}
vbc.exe /target:exe /out:MyApp.exe Employee.vb(vbc.exe 位於 .NET Framework 目錄)。/target:library (DLL)、/target:winexe (GUI)。| VB6 | VB.NET | 備註 |
|---|---|---|
| 類別模組 | Class...End Class | 支援繼承、介面、多型 |
| Set 關鍵字 | 移除,參考型別直接指派 | 所有物件皆為參考型別 |
| 預設屬性 | 僅支援一個參數的 Default Property | 建議改用索引子/方法 |
| Variant | Object | Object 為所有型別基底 |
| On Error Goto | Try...Catch...Finally | 結構化例外處理 |
| 集合 Collection | List(Of T)、Dictionary(Of K,V) | 泛型集合、型別安全、效能優 |
掌握上述核心特性,即可在 VB.NET 中撰寫結構清晰、可維護、可擴充的物件導向應用程式。