java.lang.StackOverflowError

 当子类与父类,循环调用会报次错

 

public class Father {
    protected void doSomething() {
        System.out.println("2");
         //注意这里的this在调用的实时,实际上是son的实例
        this.doSomething();
        System.out.println(this.getClass().getSimpleName());
    }

    public static void main(String[] args) {
        Father father = new Son();
        father.doSomething();
    }
}

class Son extends Father {
    @Override
    public void doSomething( ) {
        System.out.println("2" );
        super.doSomething();
    }
}

 

 java.lang.StackOverflowError_第1张图片

你可能感兴趣的:(faq,java)