【VB.NET】属性
Module Module1
Sub Main()
Dim g As Gear = New Gear(2)
Stop
End Sub
Public Class Gear
Private _m As Double
Public Sub New(m As Double)
_m = m
End Sub
Public Property M As Double
Set(value As Double)
_m = value
End Set
Get
Return _m
End Get
End Property
End Class
也可以这样使用
Module Module1
Sub Main()
Dim g As Gear = New Gear(2, 38)
Stop
End Sub
Public Class Gear
Public Sub New(_m As Double, _z As Integer)
M = _m
Z = _z
End Sub
Public Property M As Double
Public Property Z As Integer
End Class
End Module
做数据保护,不直接访问其成员。属性是变量的自然拓展,对象实例化,构造函数也会直接给属性初始化。同时改变属性值,其内部私有变量_m也会改变值。