在网页介绍一样产品的功能具备多种其他产品的功能时,就需要在这些代码上面进行合并
就例如父母两个人各具备一些优势,他们的子女也顺着拥有这些优势,可以输入以下代码:
package level02
/*
特质
trait: 实现多继承
*/
object class16 {
trait BeautifulEye {
val eye: String = "眼睛漂亮"
}
trait Tall {
val height: String = "大高个"
}
// 继承 with
class Child extends BeautifulEye with Tall {
}
def main(args: Array[String]): Unit = {
val child = new Child()
println(child.eye)
println(child.height)
}
}
运行代码结果如图所示: