Luke 1e832dc8bf test: 添加 Animal 类的 setAge 方法并优化 OS 模块
- 在 Animal 结构体中添加 setAge 方法,用于设置 age 字段
- 在 OS 模块中添加 println函数,实现换行打印功能- 更新 Main 函数,演示使用 setAge 方法和 println 函数
2025-08-30 11:28:56 +08:00

55 lines
1.2 KiB
Plaintext

module: Main
import: os
// Animal结构体
struct: Animal
// 字段
fields:
declare name: string
declare age: int
// 构造函数
init:
params:
n: string
a: int
body:
this.name = n
this.age = a
end body
end init
function: getAge
returns: int
body:
// 获取字段 age
return this.age
end body
end function
function: setAge
params:
declare a: int
body:
// 设置字段 age
this.age = a
end body
end function
end struct
// 程序入口
function: main
returns: void
body:
// 实例化一个叫A的Animal,并且调用构造函数
declare a: Animal = new Animal("GenericAnimal", 1)
// 直接调用已导入模块内的静态函数
a.setAge(2)
os.println(a.getAge())
a.setAge(3)
os.println(a.getAge())
end body
end function
end module