test: 添加 Animal 类的 setAge 方法并优化 OS 模块

- 在 Animal 结构体中添加 setAge 方法,用于设置 age 字段
- 在 OS 模块中添加 println函数,实现换行打印功能- 更新 Main 函数,演示使用 setAge 方法和 println 函数
This commit is contained in:
Luke 2025-08-30 11:28:56 +08:00
parent 513aeba2bc
commit 1e832dc8bf
2 changed files with 28 additions and 6 deletions

View File

@ -6,6 +6,7 @@ module: Main
fields:
declare name: string
declare age: int
// 构造函数
init:
params:
@ -17,11 +18,20 @@ module: Main
end body
end init
function: getAge
returns: int
function: getAge
returns: int
body:
// 获取字段 age
return this.age
end body
end function
function: setAge
params:
declare a: int
body:
// 获取字段 age
return this.age
// 设置字段 age
this.age = a
end body
end function
end struct
@ -33,7 +43,12 @@ module: Main
// 实例化一个叫A的Animal,并且调用构造函数
declare a: Animal = new Animal("GenericAnimal", 1)
// 直接调用已导入模块内的静态函数
os.print(a.getAge())
a.setAge(2)
os.println(a.getAge())
a.setAge(3)
os.println(a.getAge())
end body
end function
end module

View File

@ -1,5 +1,4 @@
module: os
// 模块级静态函数(被 import 后可直接调用)
function: print
params:
declare i1: int
@ -8,4 +7,12 @@ module: os
syscall("PRINT", i1)
end body
end function
function: println
params:
declare i1: int
returns: void
body:
syscall("PRINTLN", i1)
end body
end function
end module