VB.NET Object Oriented編程寶典
自己很喜歡編程,也喜歡上網收集一些資料,隨著VB.NET的發展,現在越多的人使用它,現在我就把我總結的一些方法給大家分享一下關于VB.NET Object Oriented編程的,大家可以記下來留著以后編程中用。
#T#VB.NET Object Oriented編程內功心法一:
也就是在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
VB.NET Object Oriented編程內功心法二:
怎樣在Instantiate Class的同時宣告和執行某些函式,例如建立一個新的SqlConnection Object或者宣告變量等等。要達到這一點,我們就利用Class的Constructors函式了。以下就是在Class里添加 Constructor函式的語法。
- Public Class CalssName
- [Public] [Sub] New ( )
- '// ...
- End Sub
- End Class
因為此是Object Oriented編程,所以也可以建立多個不同自變量的Constructor函式。但在此就跟編冩Overloads方法(Method)有點不同,那就是不需要用Overloads關鍵字來表示該函式就是Overloads函式。
- Public Class CalssName
- [Public] [Sub] New (Byval Arguement As DataType)
- '// ...
- End Sub
- End Class
VB.NET Object Oriented編程內功心法三:
有了Contructor當然就要有Destructor嘛;世界所有物軆本來是雙雙對對。。。就連Object Oriented編程也一樣,否則就不平衡了。而Destructor函式是在系統將要釋放Object時所執行,所以一般Destructor都是用來解放在整個Object里所用過(宣告和建立)的資源。
- Public Class ClassName
- [Protected] [Overrides] [Sub] Finalize ( )
- '// ...
- End Sub
- End Class