在 Scala 中为方法调用选择某个超类 [英] Select certain super class for method call in Scala
本文介绍了在 Scala 中为方法调用选择某个超类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在 Scala 中有这样一个类星座:
If I have a class constellation in Scala like this:
class A {
def foo() = "bar"
}
class B extends A {
override def foo() = "BAR"
}
class C extends B {
}
是否可以在类 C 中显式调用 A 中定义的方法 foo?
is it possible in class C to explicitly call the method foo that was defined in A?
推荐答案
不,你不能,因为它违反了封装.你绕过了你父母的行为,所以 C 应该扩展 B,但你绕过了这个.
No, you can't, because it violates encapsulation. You are bypassing your parents behaviour, so C is supposed to extend B, but you are bypassing this.
请参阅 Jon Skeets 对同一问题的出色回答 为什么是 super.super.method();在 Java 中不允许?.
Please see Jon Skeets excellent answer to the same question Why is super.super.method(); not allowed in Java?.
请注意,您可以通过反射访问该字段,但您不想这样做.
Please note that you can access the field by reflection, but you don't want to do that.
这篇关于在 Scala 中为方法调用选择某个超类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文