VB.NET Object Oriented編程寶典
自己很喜歡編程,也喜歡上網(wǎng)收集一些資料,隨著VB.NET的發(fā)展,現(xiàn)在越多的人使用它,現(xiàn)在我就把我總結(jié)的一些方法給大家分享一下關(guān)于VB.NET Object Oriented編程的,大家可以記下來留著以后編程中用。
#T#VB.NET Object Oriented編程內(nèi)功心法一:
也就是在Class里加添屬性(Properties)。有些屬性的值數(shù)只限于讀取而不能冩,有些就之能冩而不能讀?。坏话愣际莾烧呒媸?/P>
- 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
只能讀取值數(shù)的屬性:
- Public Class ClassName
- Private VeriableName As DataType
- [Public | Private | Protected] [ReadOnly] [Property] PropertyName ( ) As DataType
- Get
- '// ...
- Return VeriableName
- End Get
- End Property
- End Class
只限于冩值數(shù)的屬性:
- 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編程內(nèi)功心法二:
怎樣在Instantiate Class的同時宣告和執(zhí)行某些函式,例如建立一個新的SqlConnection Object或者宣告變量等等。要達到這一點,我們就利用Class的Constructors函式了。以下就是在Class里添加 Constructor函式的語法。
- Public Class CalssName
- [Public] [Sub] New ( )
- '// ...
- End Sub
- End Class
因為此是Object Oriented編程,所以也可以建立多個不同自變量的Constructor函式。但在此就跟編冩Overloads方法(Method)有點不同,那就是不需要用Overloads關(guān)鍵字來表示該函式就是Overloads函式。
- Public Class CalssName
- [Public] [Sub] New (Byval Arguement As DataType)
- '// ...
- End Sub
- End Class
VB.NET Object Oriented編程內(nèi)功心法三:
有了Contructor當(dāng)然就要有Destructor嘛;世界所有物軆本來是雙雙對對。。。就連Object Oriented編程也一樣,否則就不平衡了。而Destructor函式是在系統(tǒng)將要釋放Object時所執(zhí)行,所以一般Destructor都是用來解放在整個Object里所用過(宣告和建立)的資源。
- Public Class ClassName
- [Protected] [Overrides] [Sub] Finalize ( )
- '// ...
- End Sub
- End Class