首页 > 编程语言 > 详细

Java内部类的继承

时间:2018-06-08 17:10:59      阅读:177      评论:0      收藏:0      [点我收藏+]

《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告诉了我们:

技术分享图片

 

Java内部类的继承

原文:https://www.cnblogs.com/zywj/p/9156399.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!