snow/playground/Demo/Demo27/Test1.snow

75 lines
1.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 定义模块
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