创建java类中类出现is not an enclosing class

转载:http://blog.csdn.net/thirtydevs/article/details/21661485

---------------------------------------------------------------------------------------

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class A {  
  2.     public class B {  
  3.           
  4.     }  
  5. };  

需要实例B类时,按照正逻辑是,A.B ab = new A.B();

那么编译器就会出现一个错误--"is not an enclosing class"

再翻看相关的java代码,发现原来写法出错了!正确的做法是

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. A a = new A();  
  2. A.B ab = a.new B();  


没有静态(static)的类中类不能使用外部类进行.操作,必须用实例来进行实例化类中类.

猜你喜欢

转载自yuemeiqing2008-163-com.iteye.com/blog/2311068
今日推荐