java 超类不调用重写的方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/736579/
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
Does the super class not call the overridden method?
提问by Andrew Hare
I have the following classes:
我有以下课程:
class foo {
public void a() {
print("a");
}
public void b() {
a();
}
}
class bar extends foo {
public void a() {
print("overwritten a");
}
}
When I now call bar.b() I want it to call the overridden method a() in foo. It does, however, print "a".
当我现在调用 bar.b() 时,我希望它调用 foo 中的重写方法 a()。但是,它确实会打印“a”。
回答by Mike
Are your two classes in different packages? And is your foo class methods declared public, protected, or private or package local? Obviously if they are private, this won't work. Perhaps less obvious, is if they are package local (i.e. no public/protected/private scope) then you can only override them if you are in the same package as the original class.
你的两个类在不同的包中吗?您的 foo 类方法是否声明为 public、protected、private 或 package local?显然,如果它们是私有的,这将不起作用。也许不太明显的是,如果它们是本地包(即没有公共/受保护/私有范围),那么您只能在与原始类位于同一包中时覆盖它们。
For example:
例如:
package original;
public class Foo {
void a() { System.out.println("A"); }
public void b() { a(); }
}
package another;
public class Bar extends original.Foo {
void a() { System.out.println("Overwritten A"); }
}
package another;
public class Program {
public static void main(String[] args) {
Bar bar = new Bar();
bar.b();
}
}
In this case, you will still get 'A'. If you declare the original a() method in Foo public or protected, you will get the result you expected.
在这种情况下,您仍然会得到“A”。如果你在 Foo public 或 protected 中声明原始的 a() 方法,你会得到你期望的结果。
回答by CurtainDog
It may be that you are trying to use static methods, which won't work as they don't get overridden.
可能是您正在尝试使用静态方法,因为它们不会被覆盖,因此无法正常工作。
A good way of checking is to add the @Override annotation to bar.a() and see if the compiler gives you an error that a() isn't actually overidding anything
检查的一个好方法是将 @Override 注释添加到 bar.a() 并查看编译器是否给您一个错误,即 a() 实际上没有覆盖任何内容
回答by Andrew Hare
When I run the following:
当我运行以下命令时:
public class Program {
public static void main(String[] args) {
bar b = new bar();
b.b();
}
}
class foo {
public void a() {
System.out.printf("a");
}
public void b() {
a();
}
}
class bar extends foo {
public void a() {
System.out.printf("overwritten a");
}
}
I get the following output:
我得到以下输出:
overwritten a
which is what I would expect to see.
这是我希望看到的。
回答by slau
Are the methods defined as static? That's the only way I could see getting that result. I found a good explanation about that here: http://faq.javaranch.com/view?OverridingVsHiding
方法是否定义为静态?这是我能看到得到那个结果的唯一方法。我在这里找到了一个很好的解释:http: //faq.javaranch.com/view?OverridingVsHiding
回答by kenj0418
You may be confused if you are coming from C# or some other language where you have to explicitly declare virtual functions and/or overriding functions.
如果您来自 C# 或其他必须显式声明虚函数和/或覆盖函数的语言,您可能会感到困惑。
In Java, all instance functions are virtual, and can be overridden -- unless they are declared as private and/or final.
在 Java 中,所有实例函数都是虚拟的,并且可以被覆盖——除非它们被声明为 private 和/或 final。
It is not necessary to specify the new @Override annotation to do so, adding the annotation just specifies that your intent is to override, and will cause a either a warning or error if it isn't an override. (If you accidentally misspelled the method name for example).
没有必要指定新的 @Override 注释来这样做,添加注释只是指定您的意图是覆盖,如果它不是覆盖,将导致警告或错误。(例如,如果您不小心拼错了方法名称)。
Andrew's example shows how this should work.
安德鲁的例子展示了这应该如何工作。
回答by Aram Kocharyan
From the horse's mouth:
从马嘴里说:
http://download.oracle.com/javase/tutorial/java/IandI/override.html
http://download.oracle.com/javase/tutorial/java/IandI/override.html
"The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass."
“被调用的覆盖方法的版本是子类中的版本。被调用的隐藏方法的版本取决于它是从超类还是从子类调用。”
So if they are both static methods and you invoke the method from the super class, then the super class method is invoked, not the subclass method. So really, no overriding is taking place.
因此,如果它们都是静态方法,并且您从超类调用该方法,则调用的是超类方法,而不是子类方法。所以真的,没有发生覆盖。
回答by PowerAktar
While I was working on an Android program, I had the same problem on my Java classes. It turned out that the problem was not in the classes but the way Android framework processes screen output.
当我在开发一个 Android 程序时,我的 Java 类也遇到了同样的问题。原来问题不在于类,而在于 Android 框架处理屏幕输出的方式。
for example if output was programmed in the onCreate() method in the parent class, Android failed to correctly fetch the output of overridden methods from child classes beyond first child. Honestly I don't understand whole method calling order.
例如,如果输出是在父类的 onCreate() 方法中编程的,Android 无法从第一个子类之外的子类中正确获取覆盖方法的输出。老实说,我不明白整个方法调用顺序。
To resolve the issue I simply programmed the output in the onResume() and now it seem to work fine.
为了解决这个问题,我只是在 onResume() 中对输出进行了编程,现在它似乎工作正常。

