test: 添加类继承演示示例

This commit is contained in:
Luke 2025-08-31 17:55:49 +08:00
parent bd37142a4b
commit 9f7628aded
2 changed files with 74 additions and 0 deletions

View 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

View 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