FreeBasic Property 关键字
最后修改日期:2025 年 6 月 16 日
FreeBasic 的 Property 关键字用于为类成员变量创建属性访问器。属性通过 getter 和 setter 方法提供对类数据的受控访问。
基本定义
在 FreeBasic 中,Property 定义了控制类字段访问的特殊方法。属性可以具有 get 访问器(读取)、set 访问器(写入)或两者兼有。
属性通过隐藏实现细节并提供受控访问来实现封装。它们可以在访问时验证数据、计算值或触发操作。
简单的只读属性
此示例演示了一个只有 getter 的基本只读属性。
Type Person
Private:
Dim As String fullName
Public:
Declare Property Name() As String
Declare Constructor(ByVal n As String)
End Type
Constructor Person(ByVal n As String)
fullName = n
End Constructor
Property Person.Name() As String
Return fullName
End Property
Dim p As Person = Person("John Smith")
Print "Name: "; p.Name
在这里,我们定义了一个具有私有 fullName 字段的 Person 类。Name 属性提供对此字段的只读访问。构造函数初始化了私有字段。
读写属性
此示例显示了一个同时具有 get 和 set 访问器的属性。
Type Rectangle
Private:
Dim As Integer width_, height_
Public:
Declare Property Width() As Integer
Declare Property Width(ByVal w As Integer)
Declare Property Height() As Integer
Declare Property Height(ByVal h As Integer)
End Type
Property Rectangle.Width() As Integer
Return width_
End Property
Property Rectangle.Width(ByVal w As Integer)
If w > 0 Then
width_ = w
End If
End Property
Property Rectangle.Height() As Integer
Return height_
End Property
Property Rectangle.Height(ByVal h As Integer)
If h > 0 Then
height_ = h
End If
End Property
Dim rect As Rectangle
rect.Width = 100
rect.Height = 50
Print "Area: "; rect.Width * rect.Height
Rectangle 类使用属性来控制对其维度的访问。set 访问器包含验证,以确保只接受正值。这演示了数据保护。
计算属性
属性可以计算值,而不仅仅是返回存储的值。
Type Circle
Private:
Dim As Single radius
Public:
Declare Property Radius() As Single
Declare Property Radius(ByVal r As Single)
Declare Property Area() As Single
End Type
Property Circle.Radius() As Single
Return radius
End Property
Property Circle.Radius(ByVal r As Single)
radius = r
End Property
Property Circle.Area() As Single
Return 3.14159 * radius * radius
End Property
Dim c As Circle
c.Radius = 5.0
Print "Radius: "; c.Radius
Print "Area: "; c.Area
Area 属性不存储值,而是根据半径计算它。这显示了属性如何像方法一样工作,同时保持类似字段的语法。
索引属性
属性可以接受参数来创建索引属性。
Type StringArray
Private:
Dim As String arr(0 To 9)
Public:
Declare Property Item(ByVal index As Integer) As String
Declare Property Item(ByVal index As Integer, ByVal value As String)
End Type
Property StringArray.Item(ByVal index As Integer) As String
If index >= 0 And index <= 9 Then
Return arr(index)
End If
Return ""
End Property
Property StringArray.Item(ByVal index As Integer, ByVal value As String)
If index >= 0 And index <= 9 Then
arr(index) = value
End If
End Property
Dim sa As StringArray
sa.Item(2) = "FreeBasic"
sa.Item(5) = "Property"
Print sa.Item(2)
Print sa.Item(5)
此示例创建了一个索引属性,该属性充当数组访问器。该属性在访问内部数组之前验证索引范围。索引属性支持自定义集合行为。
具有副作用的只读属性
属性在访问时可以执行操作,而不仅仅是获取/设置。
Type Counter
Private:
Dim As Integer count
Dim As Integer accessCount
Public:
Declare Sub Increment()
Declare Property Value() As Integer
End Type
Sub Counter.Increment()
count += 1
End Sub
Property Counter.Value() As Integer
accessCount += 1
Print "Access #"; accessCount
Return count
End Property
Dim c As Counter
c.Increment()
c.Increment()
Print "Current value: "; c.Value
Print "Current value: "; c.Value
Value 属性跟踪其被访问的次数,同时仍然返回值。这表明属性可以产生副作用,尽管这应该谨慎使用。
具有不同访问级别的属性
属性可以为 get 和 set 操作具有不同的访问级别。
Type Account
Private:
Dim As Single balance
Public:
Declare Property Balance() As Single
Declare Sub Deposit(ByVal amount As Single)
Declare Sub Withdraw(ByVal amount As Single)
End Type
Property Account.Balance() As Single
Return balance
End Property
Sub Account.Deposit(ByVal amount As Single)
If amount > 0 Then
balance += amount
End If
End Sub
Sub Account.Withdraw(ByVal amount As Single)
If amount > 0 And amount <= balance Then
balance -= amount
End If
End Sub
Dim acc As Account
acc.Deposit(1000.0)
acc.Withdraw(200.0)
Print "Balance: $"; acc.Balance
在这里,Balance 属性在公共接口是只读的,而存款和取款通过方法处理。这种模式对于金融数据很常见,因为应该限制直接修改。
静态属性
属性可以是静态的,操作于类级别而不是实例数据。
Type Logger
Private:
Static As Integer instanceCount
Public:
Declare Static Property Count() As Integer
Declare Constructor()
End Type
Constructor Logger()
instanceCount += 1
End Constructor
Static Property Logger.Count() As Integer
Return instanceCount
End Property
Dim As Logger log1, log2, log3
Print "Logger instances: "; Logger.Count
静态 Count 属性跟踪存在的 Logger 实例的数量。静态属性在类的所有实例之间共享,并且无需实例即可访问。
最佳实践
- 封装: 使用属性来隐藏实现细节。
- 验证: 在属性的 setter 中执行验证。
- 一致性: 保持属性行为可预测。
- 性能: 避免在属性中执行昂贵的操作。
- 命名: 为属性使用清晰、描述性的名称。
- 副作用: 尽量减少属性访问器中的副作用。
本教程通过展示各种属性模式和用例的实际示例,涵盖了 FreeBasic 的 Property 关键字。