From 1e832dc8bf9ecdd62b8e69b64415f61eea89d35f Mon Sep 17 00:00:00 2001 From: Luke Date: Sat, 30 Aug 2025 11:28:56 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20Animal=20=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=20setAge=20=E6=96=B9=E6=B3=95=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20OS=20=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 Animal 结构体中添加 setAge 方法,用于设置 age 字段 - 在 OS 模块中添加 println函数,实现换行打印功能- 更新 Main 函数,演示使用 setAge 方法和 println 函数 --- playground/Demo/Demo28/Main.snow | 25 ++++++++++++++++++++----- playground/Demo/Demo28/OS.snow | 9 ++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/playground/Demo/Demo28/Main.snow b/playground/Demo/Demo28/Main.snow index d4b0fd4..1b709c3 100644 --- a/playground/Demo/Demo28/Main.snow +++ b/playground/Demo/Demo28/Main.snow @@ -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 diff --git a/playground/Demo/Demo28/OS.snow b/playground/Demo/Demo28/OS.snow index 5f9a738..7c6de6f 100644 --- a/playground/Demo/Demo28/OS.snow +++ b/playground/Demo/Demo28/OS.snow @@ -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