今天想把内部类封装起来,然后提供给其他类使用,但是出现这样的编译错误,No enclosing instance of type PrintUtil is accessible. Must qualify the allocation with an enclosing instance of type PrintUtil (e.g. x.new A() where x is an instance of PrintUtil).
结果想起来了,内部类实例化的时候要先实例化外部类,然后再使用,类似下面这样就解决了
1.Inner i = new A().new Inner();
2. A a=new A();
3. MyThread m = a.new MyThread(i, 3000);
4. a.new MyThread(i, 3000).start();
5. a.new MyThread(i, 1000).start();
本文解决了一个关于Java内部类实例化的常见问题。当尝试直接实例化内部类时遇到编译错误,文章提供了正确的实例化方法,即先实例化外部类再创建内部类对象。
333

被折叠的 条评论
为什么被折叠?



