本文介绍了斯卡拉UpperBound和LowerBound的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我尝试运行的代码:
class Student {
def printDetails = println(我是学生)
def printSomeOtherDetails = println(我爱学习)
}
class ComputerScienceStudent extends Student {
override def printDetails = println(我是一名计算机科学学生)
覆盖def printSomeOtherDetails = println(我爱Scala)
}
类InformationTechnologyStudent extends Student {
override def printDetails = println(我是信息技术学生)
覆盖def printSomeOtherDetails = println(我爱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不是类型参数的成员S
据我所知:
- Q [A< ;: B] 是指类/方法 Q 可以接收类 A 的任何对象,其中类 A 是类 B 的子类型。这称为上限。
- Q [A> ;: B] 表示类别/方法 Q 可以接受类 A 的任何对象,其中类 A 是超类 B 。这就是所谓的下限。
如果我的理解错误,请帮助我,并帮助我理解为什么会出现上述问题。谢谢你们。
解决方案你的理解没有错,但你没有遵循后果。
具体而言,如果没有提供明确的上界,所有参数实际上都有一个上界 Object 。这发生在您的类型 MyGenericClassforLowerBound 中方法 printStudentDetails 的情况下。也就是说,类型 Object 的值可以合法地作为参数传递给此方法。但是键入 Object 不会定义方法 printDetails 和 printSomeOtherDetails - 为此错误。
为了编译方法,您还需要提供合适的上限(类似于 MyGenericClassforUpperBound $ c例如:
def printStudentDetails [S>:ComputerScienceStudent< ;: Student](student:S)= {...
然而,在这种情况下应该注意,下限实际上变得多余,因为子类 Student 的任何参数都可以成功传入,因为它可以被视为类型 Student ,满足了上限 - 所以即使 InformationTechnologyStudent 和 ComputerScienceStudent 的子类都可以成功传入。这种结构在您可能传入两个不同层次结构类型的值时会更有用。
Below is the code I am trying to run:
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
}
}
the method printStudentDetails from MyGenericClassforLowerBound is creating the problem. The statements student.printDetails and student.printSomeOtherDetails are telling me
value printDetails is not a member of type parameter S
As far as I understood:
- Q[A <: B] means the class/method Q can take any objects of class A where Class A is the sub type of class B. This is called Upper Bound.
- Q[A >: B] means the class/method Q can take any objects of class A where Class A is the super type of class B. This is called Lower Bound.
Please help me if my understanding is wrong and help me to understand why the above stated problem is coming. Thanks guys.
解决方案
Your understanding is not wrong, but you haven't followed through the consequences.
Specifically, all parameters have, in effect, an upper bound of Object if no explicit upperr bound is provided. This is happening in the case of the method printStudentDetails in your type MyGenericClassforLowerBound. That is, a value of type Object could be legally passed as the parameter to this method. But type Object does not define the methods printDetails and printSomeOtherDetails - hence the error.
To make the method compile, you would need to also provide a suitable upper bound (similar to MyGenericClassforUpperBound), eg:
def printStudentDetails[S >: ComputerScienceStudent <: Student](student: S) = { ...
It should be noted in this case, however, that the lower bound effectively becomes redundant, because any parameter that subclasses Student can be passed in successfully because it can be treated as of type Student, satisfying teh upper bound - so even InformationTechnologyStudent and subclasses of ComputerScienceStudent can be passed into it successfully. This sort of construct is more useful when you might be passed in values mixing in types from two different hierarchies.
这篇关于斯卡拉UpperBound和LowerBound的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!