背景:
阅读新闻

java 嵌套类的实例化问题

  作者: 今日评论: [字体: ]

No enclosing instance of type Foo is accessible. Must qualify the allocation with an enclosing instance of type Foo (e.g. x.new A() where x is an instance of Foo).

You can get the error

 No enclosing instance of type Foo is accessible. Must qualify the allocation with an enclosing instance of type Foo (e.g. x.new A() where x is an instance of Foo).

if you try to instantiate an inner class without an instance of the outer class.

@@@ check!

WRONG

 class Foo 
 {
   private class Bar 
   {
     // stuff
   }
 }
 class Baz
 { 
   Foo.Bar aBar = new Foo.Bar();       // wrong!
 }

WRONG

 class Foo 
 {
   private class Bar 
   {
     // stuff
   }
 }
 class Baz
 { 
   Foo.Bar aBar = (new Foo).new Bar();      
 }
来源:
录入日期:[2011/11/08 22:59:00]
收藏 推荐 打印 | 录入:mikebai | 阅读:
文章评论      
正在加载评论列表...
评论表单加载中...