Java:如何访问向下两级的父类方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3118067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How do you access a parent class method two levels down?
提问by dan.codes
I have a class that extends a class that I need to overide, but I need to call that class's parent class. since I can't call super since that will execute the direct parent what is the best way to get the parent of my parent class?
我有一个类扩展了一个我需要覆盖的类,但我需要调用该类的父类。因为我不能调用 super 因为这将执行直接父级什么是获取父级父级的最佳方法?
I am sure this is a basic question, but its been a while since I have been doing any java.
我确信这是一个基本问题,但我已经有一段时间没有做任何 java.lang.
class A
{
public void myMethod()
{ /* ... */ }
}
class B extends A
{
public void myMethod()
{ /* Another code */ }
}
class C extends B
{
I need to call Class A here
{ /* Another code */ }
}
采纳答案by Jon Skeet
You don't: it violates encapsulation.
你没有:它违反了封装。
It's fine to say, "No, I don't want my own behaviour - I want my parent's behaviour" because it's assumed that you'll only do so when it maintains your own state correctly.
可以说,“不,我不想要我自己的行为 - 我想要我父母的行为”,因为它假设您只有在正确维护自己的状态时才会这样做。
However, you can't bypass your parent's behaviour - that would stop it from enforcing its own consistency. If the parent class wantsto allow you to call the grandparent method directly, it can expose that via a separate method... but that's up to the parent class.
然而,你不能绕过你父母的行为——这会阻止它强制执行自己的一致性。如果父类想让你直接调用祖父方法,它可以通过一个单独的方法公开……但这取决于父类。
回答by pakore
You can't because it has been overridden by B. There is no instance of A inside C.
不能,因为它已被 B 覆盖。 C 中没有 A 的实例。
The thing is that you want to call a method of the class A, but from which instance? You have instantiated C, which is a class with a couple of own and inherited methods from B. If B is overriding a method of A, you can't access it from C, because the method itself belongs to C (is not a method of B, it has been inherited and now it's in C)
问题是您想调用 A 类的方法,但是从哪个实例调用?你已经实例化了 C,它是一个类,有几个自己的和从 B 继承的方法。如果 B 覆盖了 A 的方法,你不能从 C 访问它,因为方法本身属于 C(不是方法的 B,它已被继承,现在它在 C)
Possible workarounds:
可能的解决方法:
B does not override myMethod()
B 不覆盖 myMethod()
C receives an instance of A and saves it as a class property.
C 接收 A 的实例并将其保存为类属性。
myMethod()
in A is static and you use A.myMethod()
(I don't recommend it)
myMethod()
在 A 是静态的,你使用A.myMethod()
(我不推荐它)
回答by pakore
Besides what pakore answered, you could also chain super.myMethod()
calls if that works for you. You will call myMethod()
from A from the myMethod()
in B.
除了 pakore 回答的内容外,super.myMethod()
如果对您有用,您还可以链接调用。您将从B 中的myMethod()
A呼叫myMethod()
。
class A {
public void myMethod() {
....
}
}
class B extends A {
public void myMethod() {
....
super.myMethod();
....
}
}
class C extends B {
public void myMethod() {
....
super.myMethod(); //calls B who calls A
....
}
}
You will eventually be calling myMethod from A, but indirectly... If that works for you.
你最终会从 A 调用 myMethod,但间接地......如果这对你有用。
回答by mdma
You can't call A's method directly. In the few cases that I've hit this, the solution was to add a method in A to expose it's implementation. E.g.
你不能直接调用 A 的方法。在我遇到的少数情况下,解决方案是在 A 中添加一个方法来公开它的实现。例如
class A
{
public myMethod() {
doAMyMethod();
}
void doAMyMethod() {
// what A want's on doMyMethod, but now it's available
// as a separately identifiable method
}
}
class C extends B
{
public void someStuff() {
this.doAMyMethod();
}
}
The scheme separates the public interface (doMyMethod) from the implementation (doAMyMethod). Inheritance does depend upon implementation details, and so this isn't strictly so bad, but I would try to avoid it if possible as it creates a tight coupling between the implementation in A and C.
该方案将公共接口 (doMyMethod) 与实现 (doAMyMethod) 分开。继承确实取决于实现细节,所以严格来说这并不是那么糟糕,但是如果可能的话,我会尽量避免它,因为它在 A 和 C 中的实现之间造成了紧密的耦合。
回答by Romain Hippeau
What you are asking is bad practice, What you are saying is that C is not a B, but an A. What you need to do is have C inherit from A. Then you can call super.
你问的是不好的做法,你说的是C不是B,而是A。你需要做的是让C继承A。然后你可以调用super。
If not this is the only way...
如果没有,这是唯一的方法......
public String A()
{
String s = "hello";
return s;
}
public String B()
{
String str = super.A();
return str;
}
public String C()
{
String s = super.B();
return s;
}
回答by susan097
What I do for this case is:
我为这个案例做的是:
Make the object of class A inside the Class C and access the Class A there. This Example which clarifies more details:
在 Class C 中创建 Class A 的对象并在那里访问 Class A。这个例子阐明了更多细节:
class A{
int a;
}
class B extends A{
int a;
}
class C extends B{
int a;
A obj = new A();
public void setData(int p,int q, int r) {
this.a = p; // points to it's own class
super.a = q;// points to one up level class i.e in this case class B
obj.a = r; // obj points the class A
}
public void getData() {
System.out.println("A class a: "+ obj.a);
System.out.println("B class a: "+ super.a);
System.out.println("C class a: "+this.a);
}
}
public class MultiLevelInheritanceSuper {
public static void main(String args[]){
C2 obj = new C2();
obj.setData(10, 20, 30);
obj.getData();
}
}
}
The output of this example is:
这个例子的输出是:
A class a: 30
B class a: 20
C class a: 10