覆盖与隐藏 Java - 困惑 [英] Overriding vs Hiding Java - Confused

查看:17
本文介绍了覆盖与隐藏 Java - 困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对覆盖与隐藏在 Java 中的区别感到困惑.任何人都可以提供有关这些差异的更多详细信息吗?我阅读了 Java 教程,但示例代码仍然让我感到困惑.

I'm confused on how overriding differs from hiding in Java. Can anyone provide more details on how these differ? I read the Java Tutorial but the sample code still left me confused.

为了更清楚,我理解覆盖很好.我的问题是我看不出隐藏有什么不同,除了一个是在实例级别而另一个在类级别.

To be more clear, I understand overriding well. My issue is that I don't see how hiding is any different, except for the fact that one is at the instance level while the other is at the class level.

看Java教程代码:

public class Animal {
    public static void testClassMethod() {
        System.out.println("Class" + " method in Animal.");
    }
    public void testInstanceMethod() {
        System.out.println("Instance " + " method in Animal.");
    }
}

然后我们有一个子类Cat:

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The class 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();
    }
}

然后他们说:

这个程序的输出如下:

Animal 中的类方法.

Class method in Animal.

Cat 中的实例方法.

The instance method in Cat.

对我来说,直接从 Animal 类调用类方法 testClassMethod() 执行Animal 类中的方法这一事实很漂亮很明显,没有什么特别的.然后他们从对 myCat 的引用调用 testInstanceMethod(),所以很明显,执行的方法是 Cat 实例中的方法>.

To me, the fact that calling a class method testClassMethod() directly from the Animal class executes the method in Animal class is pretty obvious, nothing special there. Then they call the testInstanceMethod() from a reference to myCat, so again pretty obvious that the method executed then is the one in the instance of Cat.

据我所知,调用隐藏的行为就像覆盖一样,那么为什么要区分呢?如果我使用上面的类运行此代码:

From what I see, the call hiding behaves just like overriding, so why make that distinction? If I run this code using the classes above:

Cat.testClassMethod();

我会得到:Cat 中的类方法.但是如果我从 Cat 中删除 testClassMethod() ,那么我会得到:Animal 中的类方法.

I'll get: The class method in Cat. But if I remove the testClassMethod() from Cat, then I'll get: The class method in Animal.

这向我展示了在子类中编写一个与父类具有相同签名的静态方法几乎可以进行覆盖.

Which shows me that writing a static method, with the same signature as in the parent, in a subclass pretty much does an override.

希望我能说清楚我的困惑之处,希望有人能解惑.非常感谢提前!

Hopefully I'm making clear my where I'm confused and someone can shed some light. Thanks very much in advance!

推荐答案

Overriding 基本支持后期绑定.因此,在运行时决定调用哪个方法.用于非静态方法.

Overriding basically supports late binding. Therefore, it's decided at run time which method will be called. It is for non-static methods.

隐藏适用于所有其他成员(静态方法、实例成员、静态成员).它基于早期绑定.更明确地说,要调用或使用的方法或成员是在编译时决定的.

Hiding is for all other members (static methods, instance members, static members). It is based on the early binding. More clearly, the method or member to be called or used is decided during compile time.

在您的示例中,第一次调用 Animal.testClassMethod() 是对 static 方法的调用,因此很确定将调用哪个方法.

In your example, the first call, Animal.testClassMethod() is a call to a static method, hence it is pretty sure which method is going to be called.

在第二次调用 myAnimal.testInstanceMethod() 中,您调用了一个非静态方法.这就是你所说的运行时多态性.直到运行时才决定调用哪个方法.

In the second call, myAnimal.testInstanceMethod(), you call a non-static method. This is what you call run-time polymorphism. It is not decided until run time which method is to be called.

如需进一步说明,请阅读覆盖与隐藏.

For further clarification, read Overriding Vs Hiding.

这篇关于覆盖与隐藏 Java - 困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆