简体   繁体   English

如何访问超类 toString 方法?

[英]How can I access the superclass toString method?

public class override {

    public static void main(String[] args) {
        c1 obj = new c1();
        System.out.println(obj);
    }
}

class a1 {
    public String toString (){
        return "this is clas a";
    }
}

class b1 extends a1{
    public String toString (){
        return "this is clas b";
    }
}

class c1 extends b1{
    public String toString (){
        return super.toString() + "\nthis is clas c";
    }
    
}

I need to access the superclass a1 toString method in c1 subclass.我需要访问c1子类中的超类a1 toString方法。 Is there any way to do it.有没有办法做到这一点。 I'm learning java, any help would be a great support.我正在学习 java,任何帮助都会是很大的支持。

Basically you can't - you can't access "grandparent" directly, only via the parent if it allows you to do that.基本上你不能 - 你不能直接访问“祖父母”,如果它允许你这样做,只能通过父母。

Class b1 has a toString definition that doesn't call super.toString so its class b1 that has decided to "override" the functionality of the grandparent's (a1) toString method. Class b1有一个不调用super.toStringtoString定义,因此它的 class b1决定“覆盖”祖父母的 (a1) toString方法的功能。 Since class c1 extends b1 - you "see" only this overridden version and not the version of toString of a1 .由于 class c1扩展了b1 -您“看到”的只是这个被覆盖的版本,而不是a1toString版本。

Now practically if you need this piece of functionality (lets pretends that its not toString but some code that can be required from all the children, you can do the following:现在实际上,如果您需要此功能(假设它不是 toString 而是所有孩子都可能需要的一些代码,您可以执行以下操作:

class a1 {
    protected String commonReusableMethod() {
        return "this is clas a";
    }
    public String toString (){
        return commonReusableMethod();
    }
}

class b1 extends a1{
    public String toString (){
        return "this is clas b";
    }
}

class c1 extends b1{
    public String toString (){
        return super.toString() + "\nthis is clas c" + "\n" +
        commonReusableMethod(); // you can call this method from here
    }
    
}

Note the definition of commonReusableMethod - it is protected so you can call it from anywhere in the hierarchy (from a1 , b1 , c1 )注意commonReusableMethod的定义 - 它protected ,因此您可以从层次结构中的任何位置(从a1b1c1 )调用它

If you don't want to allow overriding this protected method, add also final :如果您不想允许覆盖此受保护方法,请添加final

protected final commonReusableMethod() {...}

You would probably like to have something as super.super.toString() .您可能希望拥有super.super.toString()类的东西。 But this is not allowed in java.但这在 java 中是不允许的。 So you can simply use it twice as bellow:所以你可以简单地使用它两次:

public class override {

    public static void main(String[] args) {
        c1 obj = new c1();
        System.out.println(obj);
    }
}

class a1 {
    public String toString (){
        return "this is clas a";
    }
}

class b1 extends a1{
    public String toString (){
        return "this is clas b";
    }

    public String superToString(){
        return super.toString();
    }
}

class c1 extends b1{
    public String toString (){
        return super.superToString() + "\nthis is clas c";
    }
}

This Question may also help. 这个问题也可能有帮助。

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

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