解决No enclosing instance of type * is accessible
生活随笔
收集整理的這篇文章主要介紹了
解决No enclosing instance of type * is accessible
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
寫一個內部類,并在構造函數中初始化時,遇到報錯,搜索問題后發現,有網友出現過類似的問題,下面這個是說的淺顯明白的,并確實解決了問題。于是,以下內容照搬過來,不再多費鍵盤了。
?
public class Test_drive { public static void main(String[] args){ A a = new A(); //報錯 B b = new B(); //報錯 System.out.println(b instanceof A); } class A{ int a; } class B extends A{ } }上面兩個語句報錯信息如下:
No enclosing instance of type Test_drive is accessible. Must qualify the allocation with an enclosing instance of type Test_drive (e.g. x.new A() where x is an instance of Test_drive).在overflow上面查找到了類似的問題:http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible/9560633#9560633
下面簡單說一下我的理解:
在這里,A和B都是Test_drive的內部類,類似于普通的實例變量,如類的靜態方法不可以直接調用類的實例變量。在這里,內部類不是靜態的內部類,所以,直接賦值(即實例化內部類),所以程序報錯。
解決的方法可以有以下兩種:
(1)將內部類定義為static,即為靜態類
(2)將A a = new A();B b = new B();改為:
Test_drive td = new Test_drive(); A a = td.new A(); B b = td.new B();轉載于:https://www.cnblogs.com/data2value/p/5579472.html
總結
以上是生活随笔為你收集整理的解决No enclosing instance of type * is accessible的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ghost文件封装说明
- 下一篇: 6.24AppCan移动开发者大会价值3