test: 添加 Animal 类的 setAge 方法并优化 OS 模块
- 在 Animal 结构体中添加 setAge 方法,用于设置 age 字段 - 在 OS 模块中添加 println函数,实现换行打印功能- 更新 Main 函数,演示使用 setAge 方法和 println 函数
This commit is contained in:
parent
513aeba2bc
commit
1e832dc8bf
@ -6,6 +6,7 @@ module: Main
|
||||
fields:
|
||||
declare name: string
|
||||
declare age: int
|
||||
|
||||
// 构造函数
|
||||
init:
|
||||
params:
|
||||
@ -24,6 +25,15 @@ module: Main
|
||||
return this.age
|
||||
end body
|
||||
end function
|
||||
|
||||
function: setAge
|
||||
params:
|
||||
declare a: int
|
||||
body:
|
||||
// 设置字段 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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user