Java代码:
class P {
public void hello() {}
}
class C extends P {
public void hello(String s) {}
}
我的问题是:类 C
中的 hello
是否重载了父类(super class) P
中的同名对象?
我的 friend 说他们不是因为他们不在同一个类(class)。
最佳答案
采用更正式的方法,Java 7 的 Java 语言规范指出:
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9
我会将您的 friend 指向此链接。
所以,简而言之,在您的示例中,hello 方法确实重载了。
关于java - 子类中的方法可以重载父类(super class)中的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24884151/