【发布时间】:2020-04-30 18:08:52
【问题描述】:
我有抽象类 Person 和两个特征 Employee 和 Student
abstract class Person(val name: String) {
val tax: Double
}
trait Employee {
val salary: Double;
lazy val tax: Double = salary * 0.1;
}
trait Student {
val tax: Double = 0.0;
}
我需要使用这两个特征创建 2 个实例
studentEmployee = new Person("John") with Student with Employee {override var salary: Double = 1000};
employeeStudent = new Person("Mike") with Employee with Student {override var salary: Double = 1000};
我得到错误:
...继承冲突的成员:Double 类型的 trait Employee 中的惰性值税和 Double 类型的 trait Student 中的值税 ...
如何在同名字段中使用两个特征?
【问题讨论】:
-
定义返回值的抽象方法时,不要使用
val。使用def tax: Double