关于 docs.oracle.com 上的覆盖和隐藏方法的文本是否含糊不清?

is the text regarding overriding and hiding methods on docs.oracle.com ambiguous?

我正在 docs.oracle.com 网站 (https://docs.oracle.com/javase/tutorial/java/IandI/override.html) 上阅读有关覆盖和隐藏方法的信息。 在'Static Methods'标题下Oracle好心为我们总结一下:

“隐藏静态方法和覆盖实例方法之间的区别具有重要意义:

第一个要点是 super-clear。第二个要点似乎也提供了很多信息,但当我重读它时,我似乎感到困惑。

从我的角度来看,超类和子类中的每个静态方法似乎都被隐藏了,并且通过 early-binding(compile-time 绑定)选择了适当的方法,另一个被隐藏了.这是由您从中调用它的变量的引用类型选择的。 然而,第二个要点中句子的表述使得编译器似乎将这两个方法视为一个方法,因为它们具有相同的签名,并且具有版本 super 和版本 sub,这与必须分开隐藏的方法并选择适当的方法相反.

我的问题是这两个角度中哪一个是正确的(两种方法都是隐藏的,或者一种方法由于方法签名而有两个版本),我想哪一个重要吗? 奖励:我对这个问题的总体看法是否正确?

public class Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Animal");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Animal");
    }
}

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}

//The static method in Animal
//The instance method in Cat

From my perspective it seems like that both static methods in both the superclass and subclass are hidden, and by early-binding (compile-time binding) the appropriate method is chosen and the other one is hidden.

对于下面的代码,请考虑以下代码:

class SuperClass {
    static void a() { System.out.println("SuperClass.a()"); }
    static void b() { System.out.println("SuperClass.b()"); }
    void testSuper() { // Call from "inside" SuperClass
        a(); // calls SuperClass.a()
        b(); // calls SuperClass.b()
    }
}
class SubClass extends SuperClass {
    static void b() { System.out.println("SubClass.b()"); }
    static void c() { System.out.println("SubClass.c()"); }
    void testSub() { // Call from "inside" SubClass
        a();   // calls SuperClass.a().
        b();   // calls SubClass.b().
        c();   // calls SubClass.c()
    }
}
class Test {
    void testSuper() { // Call SuperClass from "outside"
        SuperClass.a(); // calls SuperClass.a()
        SuperClass.b(); // calls SuperClass.b()
    }
    void testSub() { // Call SubClass from "outside"
        SubClass.a(); // calls SuperClass.a()
                      // IDE warning: The static method a() from the type SuperClass should be accessed directly
        SubClass.b(); // calls SubClass.b()
        SubClass.c(); // calls SubClass.c()
    }
}

有两种情况需要考虑:

  • 两个testSuper()方法:从"inside"调用一个方法SuperClass,或者通过[=12]限定从"outside"调用一个方法=].

    在这种情况下,没有任何隐藏。您可以在 SuperClass.

    中调用 a()b()

    SubClass中的方法不被认为是隐藏的,因为它们显然不在范围内。

  • 两个testSub()方法:从"inside"调用一个方法SubClass,或者通过[=17]限定从"outside"调用一个方法=].

    在这种情况下,所有 4 个方法都在范围内,但 SuperClass 中的 b() 方法被隐藏。

如您所见,只有 SuperClass 中的 b() 被认为是隐藏的。 SubClass 中的 b() 永远不会 隐藏