如果我在Scala中有类星座,那么:
class A {
def foo() = "bar"
}
class B extends A {
override def foo() = "BAR"
}
class C extends B {
}
是否可以在C类中显式调用A中定义的方法foo?
答案 0 :(得分:17)
不,但您可以使用多个特征继承做类似的事情:
trait A { def foo = "from A" }
class B extends A { override def foo = "from B" }
class C extends B with A {
val b = foo // "from B"
val a = super[A].foo // "from A"
}
答案 1 :(得分:8)
不,你不能,因为它违反了封装。你绕过了父母的行为,所以C应该扩展B,但是你绕过了它。
请参阅Jon Skeets对同一问题的完美答案Why is super.super.method(); not allowed in Java?。
请注意,您可以通过反射访问该字段,但您不希望这样做。
答案 2 :(得分:2)
没有。我会把它留在那,但显然SO要我继续30个字符。