谁能告诉我下面的代码为什么不起作用?
public class NestedClassPrac {
public static class Nested {
public void sayHello() { System.out.println("First nested class~");}
public static class LittleNested {
public void sayHello() { System.out.println("this is why we are nested class");}
}
}
public static void main(String[] args) {
Nested a = new Nested();
a.sayHello();
LittleNested b = new LittleNested();
b.sayHello();
}
}
错误消息:
NestedClassPrac.java:13: cannot find symbol
symbol : class LittleNested
location: class NestedClassPrac
LittleNested b = new LittleNested();
^
NestedClassPrac.java:13: cannot find symbol
symbol : class LittleNested
location: class NestedClassPrac
LittleNested b = new LittleNested();
^
2 errors
LittleNested
只能通过Nested
类访问,如果不使用则不能直接访问它Nested
。可以访问内部静态类,就像访问该类的任何其他静态成员一样(即method,variable)。
例如
class X{
static class Y{
static class Z{
Z(){
System.out.println("Inside Z");
}
}
}
}
你可以创建Object
的Z
这样的内部类是静态的。
X.Y.Z obj=new X.Y.Z();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句