test: 修改 Demo28

This commit is contained in:
Luke 2025-08-29 17:41:52 +08:00
parent a8f1789fe0
commit 37c03cf248
2 changed files with 25 additions and 10 deletions

View File

@ -1,24 +1,27 @@
module: Main module: Main
// Animal结构体 import: os
// Animal结构体
struct: Animal struct: Animal
// 字段 // 字段
fields: fields:
declare name: string declare name: string
declare age: int
// 构造函数 // 构造函数
init: init:
params: params:
n: string n: string
a: int
body: body:
this.name = n this.name = n
this.age = a
end body end body
end init end init
// Animal结构体封装的函数 function: getAge
function: getName returns: int
returns: string
body: body:
// 返回字段 name // 获取字段 age
return this.name return this.age
end body end body
end function end function
end struct end struct
@ -28,8 +31,9 @@ module: Main
returns: void returns: void
body: body:
// 实例化一个叫A的Animal,并且调用构造函数 // 实例化一个叫A的Animal,并且调用构造函数
declare a: Animal = new Animal("GenericAnimal") declare a: Animal = new Animal("GenericAnimal", 1)
a.getName() // 直接调用已导入模块内的静态函数
end body os.print(a.getAge())
end body
end function end function
end module end module

View File

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