Java内部类的继承

《Thinking in Java》说:

Because the inner-class constructor must attach to a reference of the enclosing class object,
things are slightly complicated when you inherit from an inner class. The problem is that the
"secret" reference to the enclosing class object must be initialized, and yet in the derived class
there’s no longer a default object to attach to. 

package com.zywj;

class A {
    class B{
        B(String s) {
            System.out.println(s);
        }
    }
}

public class App extends A.B {
    App(A a) {
        a.super("hello");
    }
    public static void main(String[] args) {
        A a = new A();
        App app = new App(a);
    }
}

这里的a.super("hello");  有点不好理解。我一开始以为是对象a的super对象。

其实是(对象A).(对象B)("hello"),因为App是继承A.B的,所以在这里的super是指向B的。

IDEA告诉了我们:

猜你喜欢

转载自www.cnblogs.com/zywj/p/9156399.html
今日推荐