用DeEco Studio软件ArkTs定义一个接口person具有name、age、-|||-id_num属性以及方法print。创建两个类-|||-(学生、老师)分别实现上述接口
时间: 2024-10-14 16:02:44 浏览: 52
在DeEco Studio的ArkTs框架下,我们可以使用接口和类来定义一个结构清晰的系统。首先,我们需要在Person接口中声明所需的属性和方法:
```arkts
interface Person {
var name: String // 姓名
var age: Int // 年龄
var id_num: String // 身份号码
fun print() // 打印个人信息的方法
}
```
接下来,我们创建两个实现了Person接口的具体类,如学生Student和老师Teacher:
```arkts
class Student implements Person {
override var name: String = ""
override var age: Int = 0
override var id_num: String = ""
override fun print() {
println("Student: Name - $name, Age - ${age}, ID - $id_num")
}
}
class Teacher implements Person {
override var name: String = ""
override var age: Int = 0
override var id_num: String = ""
override fun print() {
println("Teacher: Name - $name, Age - ${age}, ID - $id_num")
}
}
```
在这里,`implements Person`表示这两个类都实现了Person接口,它们需要提供接口中定义的所有属性和方法。每个类都有自己的默认实现,比如打印个人信息的部分,可以根据实际需求进行修改。
阅读全文
相关推荐


















