简体   繁体   English

Java:主类中的递归调用subclass-method而不是自己的方法

[英]Java: recursion within main-class calls subclass-method instead of its own method

Example: 例:

class MainClass {
    public doIt() {
        ...
        else doIt();
    }
}

class SubClass extends MainClass {
    @Override
    public doIt() {
        super.doIt();
        ...
    }
}

Now the problem is: 现在的问题是:

  • I call SubClass.doIt() 我叫SubClass.doIt()
  • MainClass.doIt() is called 调用MainClass.doIt()
  • MainClass.doIt() makes recursion calling doIt() MainClass.doIt()进行递归调用doIt()
    But: the SubClass.doIt() is called instead of MainClass.doIt() 但是:调用SubClass.doIt()而不是MainClass.doIt()

That is very strange behaviour and problems are programmed! 这是非常奇怪的行为,编程问题! I tried to call the recursion with this.doIt() but that didn't help. 我试图用this.doIt()调用递归,但这没有帮助。 Someone has an idea? 有人有想法吗?

Thanks alot for your answers, this problem is solved. 非常感谢您的回答,这个问题已经解决了。

That's the supposed behavior, by not setting a method final , that means it can be override n, so you must always take into account someone can do this. 这是假设的行为,通过不设置方法final ,这意味着它可以override n,所以你必须始终考虑到有人可以做到这一点。 Call's to that method are never guaranteed to be to the method at that level. 对该方法的调用永远不会保证是该级别的方法。

You can however solve this problem elegantly, by using a ( protected ) final method: 但是,您可以使用( protectedfinal方法优雅地解决此问题:

class MainClass {

    protected final void innerDoIt () { //final: so no @Override
        ...
        else innerDoIt();
    }

    public void doIt() {
        innerDoIt();
    }

}

And then: 接着:

class SubClass extends MainClass {

    @Override
    public doIt() {
        super.doIt();
        ...
    }
}

final ensures, the method can't be overriden. final确保,该方法不能被覆盖。 So at that moment, you have a contract (guarantee) that the innerDoIt method is indeed the innerDoIt method you think it is. 所以在那一刻,你有一个合约(保证), innerDoIt方法确实是你认为的innerDoIt方法。

So in case you don't wan't the caller to get overriden , simply hedge it into another final method. 因此,如果您不希望调用者被覆盖 ,只需将其对冲成另一种final方法。 By making it protected , that method can also be called by the SubClass . 通过使其protected ,该方法也可以由SubClass调用。

public class Main {

    public static void main(String[] args) {
        B b = new B();
        b.doIt();
        System.out.println();
        A a = new B();
        a.doIt();
    }


    public static class A{
        boolean check=false;
        public void doIt(){
            System.out.println("A start");
            if(check){

            }
            else{
                check = true;
                doIt();
            }
            System.out.println("A end");
        }
    }
    public static class B extends A{
        @Override
        public void doIt() {
            System.out.println("B start");
            super.doIt();
            System.out.println("B end");
        }
    }
}

In this example, both b and a are instances of class B , so as you probably expect, a.doIt() and b.doIt() will output the same result. 在这个例子中, ba都是B类的实例,正如你可能期望的那样,a.doIt()和b.doIt()将输出相同的结果。

B start
A start
B start
A start
A end
B end
A end
B end

B start
A start
B start
A start
A end
B end
A end
B end

When you call doIt() , you are implicitly calling this.doIt() , and this is an instance of class B . 当你调用doIt() ,你隐式调用this.doIt()this是一个B类的实例。 There is no syntax to do what you want do without seperating the content of doIt() (see CommuSoft's answer) 如果不分离doIt()的内容,没有语法可以做你想做的事(参见CommuSoft的回答)

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

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