Koncepcja Scala UpperBound i LowerBound
Poniżej znajduje się kod, który próbuję uruchomić:
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
}
}
metodaprintStudentDetails
zMyGenericClassforLowerBound
tworzy problem. Oświadczeniastudent.printDetails
istudent.printSomeOtherDetails
mówią mi
value printDetails is not a member of type parameter S
O ile zrozumiałem:
Q[A <: B]
oznacza klasę / metodęQ
może zabrać dowolne przedmioty klasyA
gdzie klasaA
jest podtypem klasyB
. To się nazywa Upper Bound.Q[A >: B]
oznacza klasę / metodęQ
może zabrać dowolne przedmioty klasyA
gdzie klasaA
to super typ klasyB
. Nazywa się to Dolna granica.Pomóż mi, jeśli moje zrozumienie jest błędne i pomóż mi zrozumieć, dlaczego nadchodzi wyżej wymieniony problem. Dzięki chłopaki.