简体   繁体   English

在 Java 中重写方法时会发生什么?

[英]What happens when a method is overridden in Java?

When a predefined method in Java is overridden...当 Java 中的预定义方法被覆盖时...

  1. Only the method signature must be same只有方法签名必须相同
  2. Both method signature and inheritance must be same方法签名和继承必须相同

What is the answer?答案是什么? 1 or 2 1 或 2

I know that when we override the method in superclass, the method signature should be the same.我知道当我们重写超类中的方法时,方法签名应该是相同的。 But what about the inheritance?但是遗产呢? I have read this question in a book, but I did not find any answer even after some research.我在一本书上读过这个问题,但即使经过一些研究,我也没有找到任何答案。

From the docs : 从文档

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.子类中的实例方法具有与超类中的实例方法相同的签名(名称,加上其参数的数量和类型)和返回类型,将覆盖超类的方法。

The method signature must be the same;方法签名必须相同; that is, the name, parameters, position of parameters, and number of parameters must match.即名称、参数、参数位置、参数个数必须匹配。

The most common example of this is toString() , which lives on Object .最常见的例子是toString() ,它存在于Object Object 's toString method is defined as this: ObjecttoString方法定义如下:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

...whereas AbstractCollection *'s toString method is defined as this: ...而AbstractCollection * 的toString方法定义如下:

public String toString() {
    Iterator<E> it = iterator();
    if (! it.hasNext())
        return "[]";

    StringBuilder sb = new StringBuilder();
    sb.append('[');
    for (;;) {
        E e = it.next();
        sb.append(e == this ? "(this Collection)" : e);
        if (! it.hasNext())
            return sb.append(']').toString();
        sb.append(',').append(' ');
    }
}

Notice that both of the method signatures are the same, but what they return is different.请注意,这两个方法签名是相同的,但它们返回的内容不同。 This is the intention of overriding;这是覆盖的意图; your parent class has defined specific behavior for it that doesn't necessarily make sense in the children classes, so one simply overtakes that behavior and makes it suitable for the child class.您的父类为其定义了特定的行为,这在子类中不一定有意义,因此只需超越该行为并使其适合子类。

*: This is used for anything that is an AbstractCollection , like ArrayList and LinkedList . *:这用于任何属于AbstractCollection东西,例如ArrayListLinkedList


To expand a bit: the visibility of the method in the child also plays a role.扩展一点:方法在孩子中的可见性也起作用。

From this handy chart , methods that have the private modifier cannot be passed to subclasses.从这个方便的图表中,不能将具有private修饰符的方法传递给子类。

When overriding methods, you cannot reduce the visibility of a method;覆盖方法时,不能降低方法的可见性; that is, you cannot descend lower in the visibility order provided.也就是说,您不能按照提供的可见性顺序下降。

To help, here's a quick list.为了提供帮助,这里有一个快速列表。

If the method in your parent is...如果您的父级中的方法是...

  • public : the subclass' override must be public . public :子类的覆盖必须public
  • protected : the subclass' override can either be public or protected . protected :子类的覆盖可以是publicprotected
  • <no modifier> or package-private: the subclass' override can be public , protected , or package-private. <no modifier>或包私有:子类的覆盖可以是publicprotected或 package-private 。
  • private : the subclass doesn't even know the method exists . private :子类甚至不知道该方法存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM