FreeBasic Class 关键字
最后修改日期:2025 年 6 月 16 日
FreeBasic 的 Class 关键字通过定义封装数据和行为的自定义类型来启用面向对象编程。类是用于创建具有属性和方法的对象的蓝图。
基本定义
在 FreeBasic 中,Class 声明了一个用户定义的类型,它可以包含成员变量(字段)和成员函数(方法)。类支持封装、继承和多态。
类成员可以具有不同的访问级别:Public、Private 或 Protected。Public 成员可以在任何地方访问,而 Private 成员只能在类内部访问。
简单类定义
本示例演示了一个具有属性和方法的基本类。
Type Person
Public:
Dim fname As String
Dim age As Integer
Declare Sub greet()
Private:
Dim secret As String = "My secret"
End Type
Sub Person.greet()
Print "Hello, my name is "; This.fname
Print "I am "; This.age; " years old"
End Sub
Dim p As Person
p.fname = "John"
p.age = 30
p.greet()
在这里,我们定义了一个 Person 类,它具有公共字段 name 和 age,一个私有字段 secret,以及一个公共方法 greet。This 关键字引用当前对象实例。
类构造函数和析构函数
类可以具有称为构造函数和析构函数的特殊方法。
Type Book
Public:
Dim title As String
Dim author As String
Declare Constructor()
Declare Constructor(t As String, a As String)
Declare Destructor()
End Type
Constructor Book()
This.title = "Untitled"
This.author = "Unknown"
End Constructor
Constructor Book(t As String, a As String)
This.title = t
This.author = a
End Constructor
Destructor Book()
Print "Book '"; This.title; "' is being destroyed"
End Destructor
Dim b1 As Book
Dim b2 As Book = Book("The Hobbit", "J.R.R. Tolkien")
Print b1.title; " by "; b1.author
Print b2.title; " by "; b2.author
本示例展示了两个构造函数(一个默认构造函数和一个参数化构造函数)和一个析构函数。构造函数用于初始化对象,而析构函数在对象被销毁时调用。FreeBasic 会自动调用析构函数。
类继承
类可以继承自其他类以创建层次结构关系。
Type Animal
Public:
Dim dname As String
Declare Sub speak()
End Type
Sub Animal.speak()
Print "Animal sound"
End Sub
Type Dog Extends Animal
Public:
Declare Sub speak()
End Type
Sub Dog.speak()
Print This.dname; " says: Woof!"
End Sub
Dim d As Dog
d.dname = "Rex"
d.speak()
在这里,Dog 继承自 Animal 并重写了 speak 方法。Extends 关键字建立继承关系。这演示了 FreeBasic 中的多态性。
类属性
属性通过 getter 和 setter 提供对类字段的受控访问。
Type Circle
Private:
Dim radius As Single
Public:
Declare Property r() As Single
Declare Property r(ByVal value As Single)
End Type
Property Circle.r() As Single
Return This.radius
End Property
Property Circle.r(ByVal value As Single)
If value >= 0 Then
This.radius = value
Else
This.radius = 0
End If
End Property
Dim c As Circle
c.r = 5.5
Print "Radius: "; c.r
c.r = -2
Print "Radius: "; c.r
这个 Circle 类使用属性来控制对其 radius 字段的访问。setter 属性包含验证,以确保半径不能为负数。属性有助于维护数据完整性。
静态类成员
静态成员属于类本身,而不是单个实例。
Type Counter
Public:
Declare Static Sub increment()
Declare Static Function getCount() As Integer
Private:
Static As Integer count
End Type
Static As Integer Counter.count = 0
Static Sub Counter.increment()
Counter.count += 1
End Sub
Static Function Counter.getCount() As Integer
Return Counter.count
End Function
Counter.increment()
Counter.increment()
Counter.increment()
Print "Count: "; Counter.getCount()
Counter 类演示了静态成员。count 变量在所有实例之间共享。静态方法是针对类本身而不是对象调用的。请注意静态定义的特殊语法。
运算符重载
类可以为 +, -, * 等运算符定义自定义行为。
Type Vector
Public:
Dim x As Single
Dim y As Single
Declare Operator Cast() As String
Declare Operator + (ByRef v As Vector) As Vector
End Type
Operator Vector.Cast() As String
Return "(" & This.x & ", " & This.y & ")"
End Operator
Operator Vector.+ (ByRef v As Vector) As Vector
Dim result As Vector
result.x = This.x + v.x
result.y = This.y + v.y
Return result
End Operator
Dim v1 As Vector = (1, 2)
Dim v2 As Vector = (3, 4)
Dim v3 As Vector = v1 + v2
Print "v1: "; v1
Print "v2: "; v2
Print "v1 + v2: "; v3
这个 Vector 类重载了 + 运算符并提供了字符串转换运算符。运算符重载允许为自定义类型使用自然语法。转换运算符支持自动转换为字符串以便打印。
抽象类
抽象类定义了派生类必须实现的接口。
Type Shape Abstract
Public:
Declare Abstract Function area() As Single
End Type
Type Circle Extends Shape
Public:
Dim radius As Single
Declare Function area() As Single
End Type
Function Circle.area() As Single
Return 3.14159 * This.radius * This.radius
End Function
Dim c As Circle
c.radius = 5
Print "Circle area: "; c.area()
Shape 类是抽象的,带有一个抽象的 area 方法。Circle 继承自 Shape 并实现了必需的方法。抽象类不能直接实例化。
最佳实践
- 封装:将字段设为私有,并通过方法/属性公开。
- 单一职责:每个类都应有一个明确的目的。
- 命名:类名使用 PascalCase,成员使用 camelCase。
- 组合:可能时优先使用组合而非继承。
- 文档:清晰地记录公共接口。
本教程介绍了 FreeBasic 的 Class 关键字,并通过实际示例展示了 FreeBasic 中的面向对象编程技术。