Java 使用子类对象访问超类函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2432249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:33:31  来源:igfitidea点击:

Accessing super class function using subclass object

javainheritance

提问by bdhar

I have an object of a subclass extending its superclass. There is an overridden method in subclass which can be called using the object. Is that possible to call the superclass's function using the subclass object?

我有一个扩展其超类的子类的对象。子类中有一个可以使用对象调用的重写方法。是否可以使用子类对象调用超类的函数?

package supercall;

public class Main {

    public static void main(String[] args) {
        SomeClass obj = new SubClass();
        obj.go();   //is there anything like, obj.super.go()?
    }

}

class SomeClass {
    SomeClass() {

    }
    public void go() {
        System.out.println("Someclass go");
    }
}

class SubClass extends SomeClass {
    SubClass() {

    }
    @Override
    public void go() {
        System.out.println("Subclass go");
    }
}

Consider the code above.

考虑上面的代码。

Here it prints

这里打印

Subclass go

子类去

. Instead I have to print

. 相反,我必须打印

Superclass go

超班去

.

.

采纳答案by Michael Borgwardt

No, it's not possible, and if you think you need it, rethink your design. The whole point of overriding a method is to replace its functionality. If a different class knows that much about that class's internal workings, you're completely killing encapsulation.

不,这是不可能的,如果您认为需要它,请重新考虑您的设计。覆盖方法的全部意义在于替换其功能。如果一个不同的类非常了解该类的内部工作原理,那么您就完全破坏了封装。

回答by Noon Silk

Instead of:

代替:

System.out.println("Subclass go");

Write

super.go();

(Or, you know, just don't implement that method ...).

(或者,您知道,只是不要实现该方法......)。

回答by Noon Silk

Is there any way to achieve this? No, there isn't.

有没有办法实现这一目标?不,没有。

At runtime the JVM will pick the methods to call based on the instance type (that's polymorphism for you), not based on the type it is declared with.

在运行时,JVM 将根据实例类型(这对您来说是多态性)而不是根据它声明的类型来选择要调用的方法。

Your instance is a subclass instance, so the go() method in the subclass will be called.

您的实例是子类实例,因此将调用子类中的 go() 方法。

回答by Steve

One way would be to make the subclass call teh super classes implementation. But I'm assuming thats not what you want?

一种方法是让子类调用超类实现。但我假设那不是你想要的?

class SubClass extends SomeClass {    
SubClass() {}    
    @Override    
    public void go() {    
        super.go
    }
}

回答by EEE

Here it prints Subclass go . Instead I have to print Superclass go

在这里它打印 Subclass go 。相反,我必须打印 Superclass go

Alright, then do not override go method @ subclass, it will call superclass implementation.

好吧,那就不要覆盖go方法@subclass,它会调用超类实现。

If you want to run super implementation, and have some other additional code @ subclass, you call super.go(); and then run some other statements.

如果你想运行超级实现,并且有一些其他额外的代码@子类,你可以调用 super.go(); 然后运行一些其他语句。

It's ok, as you are reusing already written code, you shouldn't copy-paste code from superclass and put it into subs as it's code duplication. But if your goal is to change behaviour completely, then don't call super

没关系,因为您正在重用已经编写的代码,所以您不应该从超类复制粘贴代码并将其放入子类,因为它是代码重复。但是如果你的目标是完全改变行为,那么就不要调用 super