如果这个问题很愚蠢,请原谅我,因为我对JAVA程序完全陌生.我正在研究嵌套类的概念,并遇到以下程序.
// Demonstrate an inner class. class Outer { int outer_x = 100; void test() { Inner inner = new Inner(); inner.display(); } // this is an inner class class Inner { void display() { System.out.println("Display: outer_x = " + outer_x); } } } class NestedClass { public static void main(String args[]) { Outer outer = new Outer(); outer.test(); // Inner inner = new Outer().Inner(); // inner.display(); } }
而我的疑问是如何访问Inner
类的成员NestedClass
.在"Java - 完整参考"中,给出了它"You can, however, create an instance of Inner outside of
Outer by qualifying its name with Outer, as in Outer.Inner"
.但如果我尝试使用它,
Inner inner = new Outer().Inner(); inner.display();
这是投掷错误.所以请帮助我的专家.