test: 添加类继承演示示例
This commit is contained in:
parent
bd37142a4b
commit
9f7628aded
56
playground/Demo/Demo31/Main.snow
Normal file
56
playground/Demo/Demo31/Main.snow
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
module: Main
|
||||||
|
import: os
|
||||||
|
|
||||||
|
// 父类 Person
|
||||||
|
struct: Person
|
||||||
|
fields:
|
||||||
|
declare name: int
|
||||||
|
init:
|
||||||
|
params:
|
||||||
|
name: int
|
||||||
|
body:
|
||||||
|
this.name = name
|
||||||
|
end body
|
||||||
|
end init
|
||||||
|
|
||||||
|
function: getName
|
||||||
|
returns: int
|
||||||
|
body:
|
||||||
|
return this.name
|
||||||
|
end body
|
||||||
|
end function
|
||||||
|
end struct
|
||||||
|
|
||||||
|
// 子类 Student 继承 Person
|
||||||
|
struct: Student extends Person
|
||||||
|
fields:
|
||||||
|
declare studentId: int
|
||||||
|
init:
|
||||||
|
params:
|
||||||
|
name: int
|
||||||
|
studentId: int
|
||||||
|
body:
|
||||||
|
super(name) // 调用父类构造函数
|
||||||
|
this.studentId = studentId
|
||||||
|
end body
|
||||||
|
end init
|
||||||
|
|
||||||
|
function: getStudentId
|
||||||
|
returns: int
|
||||||
|
body:
|
||||||
|
return this.studentId
|
||||||
|
end body
|
||||||
|
end function
|
||||||
|
end struct
|
||||||
|
|
||||||
|
function: main
|
||||||
|
returns: void
|
||||||
|
body:
|
||||||
|
declare s: Student = new Student(123, 1001)
|
||||||
|
|
||||||
|
// Student 拥有父类方法
|
||||||
|
os.println(s.getName()) // 来自 Person → 123
|
||||||
|
os.println(s.getStudentId()) // 来自 Student → 1001
|
||||||
|
end body
|
||||||
|
end function
|
||||||
|
end module
|
||||||
18
playground/Demo/Demo31/OS.snow
Normal file
18
playground/Demo/Demo31/OS.snow
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
module: os
|
||||||
|
function: print
|
||||||
|
params:
|
||||||
|
declare i1: int
|
||||||
|
returns: void
|
||||||
|
body:
|
||||||
|
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
|
||||||
Loading…
x
Reference in New Issue
Block a user