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

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

问题描述

我对Overriding与隐藏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.

为了更清楚,我理解Overriding。我的问题是我没有看到隐藏是有什么不同的,除了一个是在实例级别而另一个是在类级别。

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

寻找在Java教程代码中:

Looking at the Java tutorial code:

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:

Then we have a subclass 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();
    }
}

然后他们说:


此程序的输出如下:

The output from this program is as follows:

Animal中的类方法。

Class method in Animal.

Cat中的实例方法

对我而言,调用类方法testClassMethod( )直接从Animal类中执行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 what 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中的类方法。

这告诉我在一个子类中编写一个静态方法,与父语句相同的签名,几乎可以覆盖。

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!

推荐答案

重叠基本上支持后期绑定。因此,将在运行时决定调用哪个方法。它用于非静态方法。
隐藏适用于所有其他成员(静态方法,实例成员,静态成员)。它基于早期绑定。更清楚的是,在编译期间决定要调用或使用的方法或成员。

Over-riding basically supports late binding . Therefore, which method will be called is decided at run time.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 as to which method is going to be called.

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

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

有关进一步说明,请阅读这个

For further clarification, read this.

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

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