在 Java 中如何访问 super-super 类? [内部迷你示例]
在下面的示例中,如何从 C
访问类 A
的方法 method()
?
class A {
public void method() { }
}
class B extends A{
public void method() { }
}
class C extends B{
public void method() { }
void test() {
method(); // C.method()
super.method(); // B.method()
C.super.method(); // B.method()
B.super.method(); // ERROR <- What I want to know
}
}
我得到的错误是
没有类型 B 的封闭实例 在范围内可访问
答案:不,这是不可能的。 Java 不允许。 类似问题。
In the example below, how can I access, from C
, the method method()
of the class A
?
class A {
public void method() { }
}
class B extends A{
public void method() { }
}
class C extends B{
public void method() { }
void test() {
method(); // C.method()
super.method(); // B.method()
C.super.method(); // B.method()
B.super.method(); // ERROR <- What I want to know
}
}
The error I am getting is
No enclosing instance of the type B is
accessible in scope
Answer: No, this is not possible. Java doesn't allow it. Similar question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,没有直接的方法可以做到这一点,但您始终可以尝试解决方法。
我不确定从 C 类访问 A 类中的方法的目的,但您始终可以获取该方法。
您可以在类 C 中创建类 A 的实例,如果这看起来太简单,请尝试使用反射 API...
[链接文本][1]
Extreme Java
Well, there is no direct way of doing this but you can always try workarounds.
I am not sure of the purpose of accessing method in class A from class C but you can always get hold of that method.
You could either create an instance of class A in class C and if that looks too simple, try using reflection API...
[link text][1]
Extreme Java
你不应该。
如果要访问
A
中的方法,请从A
而不是B
扩展。当
B
扩展A
时,它假定底层A
对象不会以除其自身操作方式之外的其他方式进行操作。因此,通过直接访问A
的方法,您可能会破坏B
的功能。例如,想象一下,您有一个实现列表的类
MyList
。现在,假设我们使用另一个名为MyCountingList
的类扩展此列表,该类重写add()
和remove()
方法来对元素进行计数添加/删除。如果您绕过MyCountingList
提供的add()
方法,而使用MyList
所提供的方法,那么您现在就破坏了MyCountingList
的计数功能>我的计数列表。所以,简而言之,就是不要。
You shouldn't.
If you want to access the methods in
A
, extend fromA
instead ofB
.When
B
extendsA
, it assumes that the underlyingA
-object won't be manipulated in other ways than how it does it itself. Therefore, by directly accessing the methods ofA
, you could be breaking howB
functions.Imagine, for instance, you have a class that implements a list,
MyList
. Now, imagine we extend this list with another class calledMyCountingList
, which overrides theadd()
andremove()
methods to count the elements being added/removed. If you bypass theadd()
methodMyCountingList
provides, using the oneMyList
has instead, you've now broken the counting feature ofMyCountingList
.So, in short, just don't.
你不能——而且是非常故意的。这会违反封装性。您将跳过
B.method
想要执行的任何操作 - 可能会验证参数(假设有任何参数)、强制执行不变量等。您如何期望
B
保持一致如果任何派生类可以跳过它定义的任何行为,那么它的世界观是什么?如果 B 提供的行为不适合 C,则不应扩展它。不要试图像这样滥用继承。
You can't - and very deliberately. It would violate encapsulation. You'd be skipping whatever
B.method
wants to do - possibly validating arguments (assuming there were any), enforcing invariants etc.How could you expect
B
to keep a consistent view of its world if any derived class can just skip whatever behaviour it's defined?If the behaviour B provides isn't appropriate for C, it shouldn't extend it. Don't try to abuse inheritance like this.
以下代码可能是一种解决方法(不太好,但应该可行):
Following code could be a work-around (not nice, but should work):