test: 重构 Demo27 模块的示例代码

This commit is contained in:
Luke 2025-08-28 17:47:46 +08:00
parent 3583e9c67c
commit f75af0dc9b
3 changed files with 209 additions and 64 deletions

View File

@ -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

View File

@ -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

View File

@ -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