免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
Board logo

標題: 屬性宣告運用 [打印本頁]

作者: mhfo    時間: 2014-4-9 10:43     標題: 屬性宣告運用

參考連結



'VB.NET : static Property
Imports System
Class MyClass
Private Shared x As Integer           '<== 關鍵句,經SHARED宣告後~鎖有產生的新 MyClacc物   件共用x屬性
Public Shared Property X() As Integer
Get
Return x
End Get
Set(ByVal Value As Integer)
x = value
End Set
End Property
End Class 'MyClass
Class MyClient
Public Shared Sub Main()
MyClass.X = 10
Dim xVal As Integer =MyClass .X
Console.WriteLine(xVal) 'Displays 10
End Sub 'Main
End Class 'MyClient

---------------------------------------
Properties & Inheritance

The properties of a Base class can be inherited to a Derived class.

'VB.NET : Property : Inheritance
Imports System
Class Base
Public Property X() As Integer
Get
Console.Write("Base GET")
Return 10
End Get
Set(ByVal Value As Integer)
Console.Write("Base SET")
End Set
End Property
End Class 'Base
Class Derived
Inherits Base
End Class 'Derived
Class MyClient
Public Shared Sub Main()
Dim d1 As New Derived
d1.X = 10
Console.WriteLine(d1.X) 'Displays 'Base SET Base GET 10'
End Sub 'Main
End Class 'MyClient

----------------------------------------------
Properties & Polymorphism

A Base class property can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, override etc are using at property level, not at accessor level.

'VB.NET : Property : Polymorphism
Imports System
Class Base
Public Overridable Property X() As Integer
Get
Console.Write("Base GET")
Return 10
End Get
Set(ByVal Value As Integer)
Console.Write("Base SET")
End Set
End Property
End Class 'Base
Class Derived
Inherits Base
Public Overrides Property X() As Integer
Get
Console.Write("Derived GET")
Return 10
End Get
Set(ByVal Value As Integer)
Console.Write("Derived SET")
End Set
End Property
End Class 'Derived
Class MyClient
Public Shared Sub Main()
Dim b1 = New Derived
b1.X = 10
Console.WriteLine(b1.X) 'Displays 'Derived SET Derived GET 10'
End Sub 'Main
End Class 'MyClient


-------------------------------------------------------------
Abstract Properties

A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.

If the abstract class contains only set accessor, we can implement only set in the derived class.

The following program shows an abstract property in action.

'VB.NET : Property : Abstract
Imports System
MustInherit Class Abstract
Public MustOverride Property X() As Integer
Get
End Get
Set
End Set
End Property
End Class 'Abstract
Class Concrete
Inherits Abstract
Public Overrides Property X() As Integer
Get
Console.Write(" GET")
Return 10
End Get
Set(ByVal Value As Integer)
Console.Write(" SET")
End Set
End Property
End Class 'Concrete
Class MyClient
Public Shared Sub Main()
Dim c1 As New Concrete
c1.X = 10
Console.WriteLine(c1.X) 'Displays 'SET GET 10'
End Sub 'Main
End Class 'MyClient

The properties are an important features added in language level inside VB.NET. They are very useful in GUI programming. Remember that the compiler actually generates the appropriate getter and setter methods when it parses the VB.NET property syntax.




歡迎光臨 魚骨頭的雲端圖書館 (http://mhfo.hotbbs.info/) Powered by Discuz! 7.0.0