简体   繁体   English

如何使用层次结构父类的方法?

[英]How to use method from parent class of hierarchy?

Using the following code: 使用以下代码:

public class Animal {
    public void a() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {   
    public void a() {
        System.out.println("Cat");
    }
}

public class BlackCat extends Cat {
    public static void main(String[] args) {

        BlackCat blackCat = new BlackCat();
        blackCat.a();
    }
}

How to use in child class BlackCat method a() from Animal but not from Cat ? 如何在儿童类BlackCat方法中使用a()来自Animal而不是来自Cat
I want to receive Animal in console. 我想在控制台中收到Animal

I guess you could add an addition function with a set prefix (this case _) and use that as your "super accessor" or what ever you want to call it. 我想你可以添加一个带有set前缀的加法函数(本例_),并将其用作“超级访问者”或者你想要调用的东西。

public class Cat extends Animal {   
    public void a() {
        System.out.println("Cat");
    }
    public void _a() {
        super.a();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM