Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归调用super上的函数_Java_Inheritance_Superclass_Super - Fatal编程技术网

Java 递归调用super上的函数

Java 递归调用super上的函数,java,inheritance,superclass,super,Java,Inheritance,Superclass,Super,我想递归地调用super上的一个方法,以便将方法调用传播到最后一个超类。这个伪代码告诉我要做什么,但当然这不是用Java编译的 public MyClass { protected void method() { // do something on this level if (super instanceof MyClass) { MyClass superLevel = (MyClass) super;

我想递归地调用super上的一个方法,以便将方法调用传播到最后一个超类。这个伪代码告诉我要做什么,但当然这不是用Java编译的

public MyClass {

    protected void method() {

        // do something on this level

        if (super instanceof MyClass) {
            MyClass superLevel = (MyClass) super;
            superLevel.method();
        }
    }
}

如何实现这种行为?

在您可以编辑所有这些类之前,您不能直接这样做。只能直接使用直接父级的方法,而不能使用上述方法


如果您可以编辑父类,则可以在每个级别上调用super.method。

在您可以编辑所有这些类之前,您不能直接这样做。只能直接使用直接父级的方法,而不能使用上述方法

如果您可以编辑父级,请在每个级别调用super.method。

您可以使用:

public class MySuperClass {
    protected void method() {
         // Whatever you want to do in super class
    }
}

public class MyClass extends MySuperClass {
    @Override
    protected void method() {
         // Whatever you want to do in this specific class

         // Call super.method
         super.method();
    }
}
您可以在类链中以这种方式使用它。

您可以使用:

public class MySuperClass {
    protected void method() {
         // Whatever you want to do in super class
    }
}

public class MyClass extends MySuperClass {
    @Override
    protected void method() {
         // Whatever you want to do in this specific class

         // Call super.method
         super.method();
    }
}

您可以这样在类链中使用它。

此代码必须放在超级类中。在超级类中,您可以编写MyClass但IMHO的这个实例,它揭示了一个设计缺陷:超级类不应该知道它的子类。唯一的选择是在每个级别上添加super.method调用。只能使用super调用立即超类方法。此代码必须放在超类中。在超级类中,您可以编写MyClass但IMHO的这个实例,它揭示了一个设计缺陷:超级类不应该知道它的子类。唯一的选择是在每个级别上添加super.method调用。您只能使用super调用立即超类方法。为什么不解释就进行向下投票?忽略它们,它们是无效的。继续帮助:为什么不解释就进行向下投票?忽略它们,它们是无效的。继续帮助: