java - java中的嵌套类

标签 java nested-class

有人可以告诉我为什么下面的代码不起作用吗?

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

最佳答案

        Nested.LittleNested b = new Nested.LittleNested();

你到底想做什么?

关于java - java中的嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178683/

相关文章:

python - (Python) 意外创建的闭包

Spring MVC 嵌套对象验证

java - 无效操作 : result set is closed

java - 如何使用 Java 驱动程序检索现有 MongoDB 集合的 validator ?

Java GUI 图像大小

java - 如何在 mongodb 中使用 where 条件从集合中获取不同的值?

java - Dagger 2 构建在单个 Inject 注释上失败

Java 注释 - javac 编译器错误?

c++ - 设计嵌套模板类

c# - 映射到嵌套类