From f75af0dc9bda3091dee1074ac7f701227b82930e Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 28 Aug 2025 17:47:46 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E9=87=8D=E6=9E=84=20Demo27=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9A=84=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playground/Demo/Demo27/Main.snow | 64 -------------- playground/Demo/Demo27/Test1.snow | 74 ++++++++++++++++ playground/Demo/Demo27/Test2.snow | 135 ++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 64 deletions(-) delete mode 100644 playground/Demo/Demo27/Main.snow create mode 100644 playground/Demo/Demo27/Test1.snow create mode 100644 playground/Demo/Demo27/Test2.snow diff --git a/playground/Demo/Demo27/Main.snow b/playground/Demo/Demo27/Main.snow deleted file mode 100644 index 117b918..0000000 --- a/playground/Demo/Demo27/Main.snow +++ /dev/null @@ -1,64 +0,0 @@ -module: Main - // 全局变量,属于整个模块 - globals: - declare c: int = 10 - - // 定义 Animal 结构体 - struct: Animal - fields: - name: string // 动物名字 - age: int // 动物年龄 - end fields - - // 内部私有函数(以下划线开头),拼接输出字符串 - function: _formatSound - returns: string - body: - return name + " makes some sound (c=" + to_string(c) + ")" - end body - end function - - // 公共方法,调用 _formatSound 并输出 - function: sound - returns: void - body: - os.print(_formatSound()) - end body - end function - end struct - - // 定义 Dog 结构体,继承 Animal - struct: Dog extends Animal - fields: - breed: string // 狗的品种,例如 Bulldog - end fields - - // 重写 _formatSound,让 Dog 有自己的叫声 - function: _formatSound - returns: string - body: - return name + " the " + breed + " says Woof!" - end body - end function - - // 重写 sound 方法,输出 Dog 的叫声 - function: sound - returns: void - body: - os.print(_formatSound()) - end body - end function - end struct - - // 程序入口 - function: main - returns: void - body: - declare d: Dog = new Dog() - d.name = "Buddy" - d.age = 3 - d.breed = "Bulldog" - d.sound() - end body - end function -end module diff --git a/playground/Demo/Demo27/Test1.snow b/playground/Demo/Demo27/Test1.snow new file mode 100644 index 0000000..9f4233e --- /dev/null +++ b/playground/Demo/Demo27/Test1.snow @@ -0,0 +1,74 @@ +// 定义模块 +module: Main + // 定义一个结构体 Counter + struct: Counter + fields: + declare value: int + + // 构造方法 + init: + params: + v: int + body: + this.value = v + end body + end init + + // 方法:打印 value + function: printValue + returns: void + body: + os.print(this.value) + end body + end function + + // 方法:打印 value + 100 + function: printSum + returns: void + body: + os.print(this.value + 100) + end body + end function + end struct + + + // 定义 AdvancedCounter,继承 Counter + struct: AdvancedCounter extends Counter + fields: + declare offset: int + + // 构造方法:调用父类构造(使用 init) + init: + params: + v: int + o: int + body: + super(v) // 调用父类构造方法 + this.offset = o + end body + end init + + // 重写 printSum:打印 value + offset + function: printSum + returns: void + body: + os.print(this.value + this.offset) + end body + end function + end struct + + + // 程序入口 + function: main + returns: void + body: + declare c: Counter = new Counter(42) + c.printValue() // 打印 42 + c.printSum() // 打印 142 + + declare ac: AdvancedCounter = new AdvancedCounter(42, 200) + ac.printValue() // 打印 42 (继承自父类) + ac.printSum() // 打印 242 (子类重写) + end body + end function +end module diff --git a/playground/Demo/Demo27/Test2.snow b/playground/Demo/Demo27/Test2.snow new file mode 100644 index 0000000..17613d6 --- /dev/null +++ b/playground/Demo/Demo27/Test2.snow @@ -0,0 +1,135 @@ +// 演示 Snow OOP 四大要点:封装、继承、多态、重载 +module: Main + // 封装:基类 Animal + struct: Animal + fields: + declare name: string + + init: + params: + n: string + body: + this.name = n + end body + end init + + // 封装:提供只读访问器 + function: getName + returns: string + body: + return this.name + end body + end function + + // 多态:基类方法,默认虚 + function: speak + returns: void + body: + os.print(this.name + " makes a sound.") + end body + end function + + // 重载示例:同名 eat,不同参数 + function: eat + params: + food: string + returns: void + body: + os.print(this.name + " eats " + food) + end body + end function + + function: eat + params: + times: int + returns: void + body: + os.print(this.name + " eats " + times + " times.") + end body + end function + end struct + + + // 继承 + 多态:Dog + struct: Dog extends Animal + init: + params: + n: string + body: + super(n) + end body + end init + + // 重写 speak + function: speak + returns: void + body: + os.print(this.name + " says: Woof!") + end body + end function + end struct + + + // 继承 + 多态:Cat + struct: Cat extends Animal + init: + params: + n: string + body: + super(n) + end body + end init + + // 重写 speak + function: speak + returns: void + body: + os.print(this.name + " says: Meow!") + end body + end function + end struct + + + // 多态演示函数 + function: letAnimalSpeak + params: + a: Animal + returns: void + body: + // 调用多态方法 + a.speak() + end body + end function + + + // 程序入口 + function: main + returns: void + body: + // 基类结构体 + declare a: Animal = new Animal("GenericAnimal") + a.speak() // -> GenericAnimal makes a sound. + a.eat("food") // -> GenericAnimal eats food + a.eat(2) // -> GenericAnimal eats 2 times. + + // 子结构体 Dog + declare d: Dog = new Dog("Buddy") + d.speak() // -> Buddy says: Woof! + d.eat("bone") // 调用基类 eat(String) + d.eat(3) // 调用基类 eat(int) + + // 子类结构体 Cat + declare c: Cat = new Cat("Kitty") + c.speak() // -> Kitty says: Meow! + c.eat("fish") + c.eat(1) + + // 多态:父类引用指向子类结构体 + declare poly: Animal = new Dog("Max") + letAnimalSpeak(poly) // -> Max says: Woof! + + poly = new Cat("Luna") + letAnimalSpeak(poly) // -> Luna says: Meow! + end body + end function +end module