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:
|
fields:
|
||||||
declare name: string
|
declare name: string
|
||||||
declare age: int
|
declare age: int
|
||||||
|
|
||||||
// 构造函数
|
// 构造函数
|
||||||
init:
|
init:
|
||||||
params:
|
params:
|
||||||
@ -17,11 +18,20 @@ module: Main
|
|||||||
end body
|
end body
|
||||||
end init
|
end init
|
||||||
|
|
||||||
function: getAge
|
function: getAge
|
||||||
returns: int
|
returns: int
|
||||||
|
body:
|
||||||
|
// 获取字段 age
|
||||||
|
return this.age
|
||||||
|
end body
|
||||||
|
end function
|
||||||
|
|
||||||
|
function: setAge
|
||||||
|
params:
|
||||||
|
declare a: int
|
||||||
body:
|
body:
|
||||||
// 获取字段 age
|
// 设置字段 age
|
||||||
return this.age
|
this.age = a
|
||||||
end body
|
end body
|
||||||
end function
|
end function
|
||||||
end struct
|
end struct
|
||||||
@ -33,7 +43,12 @@ module: Main
|
|||||||
// 实例化一个叫A的Animal,并且调用构造函数
|
// 实例化一个叫A的Animal,并且调用构造函数
|
||||||
declare a: Animal = new Animal("GenericAnimal", 1)
|
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 body
|
||||||
end function
|
end function
|
||||||
end module
|
end module
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
module: os
|
module: os
|
||||||
// 模块级静态函数(被 import 后可直接调用)
|
|
||||||
function: print
|
function: print
|
||||||
params:
|
params:
|
||||||
declare i1: int
|
declare i1: int
|
||||||
@ -8,4 +7,12 @@ module: os
|
|||||||
syscall("PRINT", i1)
|
syscall("PRINT", i1)
|
||||||
end body
|
end body
|
||||||
end function
|
end function
|
||||||
|
function: println
|
||||||
|
params:
|
||||||
|
declare i1: int
|
||||||
|
returns: void
|
||||||
|
body:
|
||||||
|
syscall("PRINTLN", i1)
|
||||||
|
end body
|
||||||
|
end function
|
||||||
end module
|
end module
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user