下面是我试图运行的代码:
class Student {
def printDetails = println("I am a student")
def printSomeOtherDetails = println("I love Studying")
}
class ComputerScienceStudent extends Student {
override def printDetails = println("I am a Computer Science Student")
override def printSomeOtherDetails = println("I love Scala")
}
class InformationTechnologyStudent extends Student {
override def printDetails = println("I am an Information Technology Student")
override def printSomeOtherDetails = println("I love Java")
}
class MyGenericClassForUpperBound {
def printStudentDetails[S <: Student](student: S) = {
student.printDetails
student.printSomeOtherDetails
}
}
class MyGenericClassforLowerBound {
def printStudentDetails[S >: ComputerScienceStudent](student: S) = {
student.printDetails
student.printSomeOtherDetails
}
}
printStudentDetails
来自的方法MyGenericClassforLowerBound
正在制造问题。陈述student.printDetails
和student.printSomeOtherDetails
告诉我
value printDetails is not a member of type parameter S
据我了解:
Q[A <: B]
表示类/方法Q
可以采用类的任何对象,A
其中 ClassA
是 class 的子类型B
。这称为上限。Q[A >: B]
表示类/方法Q
可以采用类的任何对象,A
其中 ClassA
是 class 的超类型B
。这称为下限。
如果我的理解有误,请帮助我,并帮助我理解为什么会出现上述问题。多谢你们。