1
假设我们有一个类对和继承的一些树:类型上限和继承
class Pair[ T <: Comparable[T] ](r:T,t:T) {
def sizeRelationship = r.compareTo(t)
}
sealed class Person(val importance:Int) extends Comparable[Person] {
override def compareTo(o: Person): Int = ???
}
class Student extends Person(10)
class Teacher extends Person(50)
现在如何修改上面能够打造一双[学生]:?
val mixed: Pair[Person] = new Pair(new Student,new Teacher) /* compiles */
val notMixed = new Pair[Student](new Student, new Student) /* Student does not conform to Comparable */
寻找线索:http://docs.scala-lang.org/tutorials/tour/lower-type-bounds.html – Ashalynd
你不能创建_any_'Pair [Student]',因为'Student'违反了类型'对''T'的要求。 '学生不是<:可比[学生]',但只有'<:可比[人]'。 –