java - 子类中的方法可以重载父类(super class)中的方法吗?

标签 java overloading

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/

相关文章:

c# - 在类定义中强制偏好重载?

java - 如何知道表单提交是否成功?

c++ - 根据表达式的有效性选择要应用的函数

java - 静态和非静态同步方法的并发执行有问题吗?

Java Mockito 何时返回对象创建

delphi - 如何根据结果类型重载函数?

java - 方法重载和传递 null

java : "Class Overloading"

java - SecureRandom 的 Android 实现是否会产生真正的随机数?

java - 为什么我没有关于页面上下文的异常?