Java继承:覆盖或隐藏的方法 [英] Java Inheritance: Overwritten or hidden methods

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

问题描述

当一个类扩展另一个类时,它将继承超类的所有方法和变量.如果您在具有相同签名的子类中对其进行不同定义,则方法和变量在子类中均可以不同地使用. 现在,Oracle在覆盖和隐藏之间有所区别(http://docs.oracle.com/javase/tutorial/java/IandI/override.html). 它说实例方法将覆盖其超类的方法,而类方法则将其隐藏. 隐藏和重写之间的区别具有重要意义.被调用的重写方法的版本是子类中的版本.被调用的隐藏方法的版本取决于是从超类还是子类中调用."

When a class extends another, it inherits all methods and variables of the superclass. Both methods and variables can be used differently in the subclass, if you define it differently in the subclass with the same signature. Now Oracle distincts between overwriting and hiding (http://docs.oracle.com/javase/tutorial/java/IandI/override.html). It says that an instance method overwrites its superclass's method, while a class method hides it. "The distinction between hiding and overriding has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass."

让我们假设我有2个类,是和也许.是的,也许延伸. 也许有String a.

Lets assume I have 2 classes Yes and Maybe. Yes extends Maybe. Maybe has String a.

class Maybe {
    String a;
    public static void printOut() {
        System.out.println("Maybe");
    }
    public void printAndSet() {
        a = "Maybe";
        System.out.println(a);
    }

}
class Yes extends Maybe {
    public static void printOut() {
        System.out.println("Yes");
    }
    pubilc void printAndSet() {
         a = "Yes";
    }
}
class Print{
    public static void mail(String[] args) {
        Maybe m = new Maybe();
        Yes y = new Yes();
        Maybe.printOut();
        Yes.printOut();
        m.printAndSet();
        y.printAndSet();
}

我说:它将打印出来 可能是 是的 可能是 是的

And I say: It will print out maybe yes maybe yes

但是,当我阅读Oracle文章后,我认为它必须打印出来:

But after I read the Oracle article I thought it would have to print out:

yes
yes
maybe
yes

因为实例方法会覆盖其超类方法.

Because the instance method overwrites its superclass method.

我很确定我的输出是正确的,但是我也肯定,Oracle知道 更好,所以我认为我只是不理解这篇文章. 当我从超类的对象调用实例方法时,它使用的是覆盖方法,这是不对的. 所以我不明白为什么要区分覆盖和隐藏! 有人可以帮忙吗?

I'm quite sure I am right with the output, but I'm sure aswell, that Oracle knows better so I'm thinking I just didn't understand the article. It can't be true that when I call an instance method from an object of a superclass, that it uses the overwritten method. So I do not understand why to distinguish overwriting and hiding! Can someone help out?

编辑;插入代码而不是描述类!

Edit; Inserted code instead of describing the classes!

推荐答案

静态方法根本不能被覆盖.它们不是多态调用的,因为它们不作用于类的实例,而是作用于类本身.

Static methods can't be overridden at all. They're not called polymorphically, since they don't act on an instance of the class, but on the class itself.

如果调用Maybe.printOut(),它将调用在Maybe中定义的静态printOut()方法.在Yes中还定义了一个方法printOut()的事实是无关紧要的:这两个方法除了名称外没有其他共同点.

If you call Maybe.printOut(), it will call the static printOut() method defined in Maybe. The fact that there is also a method printOut() defined in Yes is irrelevant: those two methods have nothing in common, except their name.

请注意,您可以通过简单地编写程序并执行它来确认或证实您的疑问.

Note that you could confirm or infirm your doubts by simply writing a program and executing it.

仅当您开始在对象的实例上调用静态方法时,才会出现隐藏方法的问题.这是非常糟糕的做法,绝不应该这样做.如果您不遵守此规则,请执行以下操作:

The problem with hiding methods only occurs when you start calling static methods on an instance of an object. This is very bad practice, and should never be done. If you don't respect this rule, and have the following:

Maybe m = new Maybe();
Maybe y = new Yes();

m.printOut(); // DON'T DO THAT: it should be Maybe.printOut();
y.printOut(); // DON'T DO THAT: it should be Maybe.printOut() or Yes.printOut();

结果将为maybe maybe,因为在静态方法的情况下,计数是不是对象的具体类型(MaybeYes),而是它们的已声明的类型(MaybeMaybe).

the result will be maybe maybe, because in the case of static methods, what counts is not the concrete type of the objects (Maybe and Yes), but their declared type (Maybe and Maybe).

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

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