每當我們提到Object Oriented編程時就好自然想起Class
假設用一輛汽車來跟Class相比
所以在還沒開始設計一個Class時
此篇隨筆的重點《VB
內功心法一
首先就是要寫一個最基本Class的空殻
Public Class ClassName
End Class
內功心法二
要怎樣在Class裡宣告Data Member
Public 代表屬性
Private 屬性
Protected 屬性
Public Class ClassName
[Public | Private | Protected] Variable As DataType
End Class
內功心法三
加添方法進Class裡面
Public Class ClassName
[Public | Private | Protected] [Overloads] [Sub] MethodName (Byval Arguement As DataType
End Sub
End Class
Public Class ClassName
[Public | Private | Protected] [Overloads] [Function] MethodName (Byval Arguement As DataType
Return Value
End Function
End Class
而Overloads關鍵字就是用來建立多個同名稱但自變量不同的函式
內功心法四
也就是在Class裡加添屬性(Properties)
Public Class ClassName
Private VeriableName As DataType
[Public | Private | Protected] [Property] PropertyName ( ) As DataType
Get
Return VeriableName
End Get
Set (ByVal Value As DataType)
VeriableName = Value
End Set
End Property
End Class
只能讀取值數的屬性
Public Class ClassName
Private VeriableName As DataType
[Public | Private | Protected] [ReadOnly] [Property] PropertyName ( ) As DataType
Get
Return VeriableName
End Get
End Property
End Class
只限於冩值數的屬性
Public Class ClassName
Private VeriableName As DataType
[Public | Private | Protected] [WriteOnly] [Property] PropertyName ( ) As DataType
Set (ByVal Value As DataType)
VeriableName = Value
End Set
End Property
End Class
內功心法五
怎樣在Instantiate Class的同時宣告和執行某些函式
Public Class CalssName
[Public] [Sub] New ( )
End Sub
End Class
因為此是Object Oriented編程
Public Class CalssName
[Public] [Sub] New (Byval Arguement As DataType)
End Sub
End Class
內功心法六
有了Contructor當然就要有Destructor嘛
Public Class ClassName
[Protected] [Overrides] [Sub] Finalize ( )
End Sub
End Class
內功心法七
往往我們會選擇宣告Public變量方法和外界(Class與Class之間)分享其值數
Public Class ClassName
[Public] [Shared] Variable As DataType
End Class
內功心法八
可以在Class裡共享變量
Public Class ClassName
[Public] [Shared] [Sub] MethodName (Byval ArguementAs DataType
End Sub
End Class
Public Class ClassName
[Public] [Shared] [Function] MethodName (Byval Arguement As DataType
Return Value
End Function
End Class
內功心法九
我想Inheritance也應該是所有VB編程員夢寐以求的另一突破!在過去的VB
那就拿圖一來做個例子
基本上當開始冩一個Class時(內功心法一)就可以用NotInheritable和MustInherit關鍵字來代表該Class是否能Inherit或者一定要Inherit才可以用(也就是該Class不能用New語法來Instantiate)
不能Inherit的Base Class
Public [NotInheritable] BaseClassName
End Class
一定要Inherit才能使用的Base Class
Public [MustInherit] BaseClassName
End Class
內功心法十
Polymorphism可以說是Object Oriented編程的最高境界
Polymorphism有何過人之處?引起人人掙著去學呢?那就要重Inheritance裡說起了
要用Polymorphism此功能
其一
From:http://tw.wingwit.com/Article/program/net/201311/12052.html