物件導向程式設計 Object Oriented Programming (OOP)
◎ 抽象 Abstraction
◎ 封裝 Encapsulation
◎ 多型 Polymorphism
◎ 繼承 Inheritance

1. 定義類別
Public Class Customer

End Class

2. 加入屬性 Property 
    (屬性名稱為 Name)
Private m_sName As String
Property Name() As String
    Get
        Return m_sName
    End Get
    Set ( Byval value As String)
        m_sName = Value
    End Set
End Property

3. 加入方法 Method
Public Function SayHello() As String
    If Name <> "" Then
        Return "Hi!" & Name
    Else
        Return "Hi! Everyone!"
    End If
End Function

4. 建立建構函式 Constructor
    在 Initialize 事件中撰寫初始化物件的程式碼
Public Sub New ()
    Debug.WriteLine ("I am Ready!")
End Sub

Public Sub New ( ByVal sName As String)
    Name = sName
    Debug.WriteLine (Name & " is Ready!")
End Sub
    § 多載 overloading:可替同一類別定義多個建構函式,只要其均擁有不同的參數即可。
    § 若未建立任何建構函式,會自動使用一個預設建構函式。

5. 建立解構函式 Destructor
    當記憶體回收行程確認此物件不再需要時,回呼叫 Finalize 解構函式。但在一個物件被終結與記憶體回收行程實際釋放之間會有時間上的落差,故應實作 IDisposable 介面與 Dispose 解構函式。
Implements IDisposable
Public Sub Dispose () Implements System.IDisposable.Dispose

End Sub
arrow
arrow
    全站熱搜

    tsuozoe 發表在 痞客邦 留言(0) 人氣()