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'的要求。 '學生不是<:可比[學生]',但只有'<:可比[人]'。 –