最近在编写例子的时候,自己添加了一个内部类。结果编译时遇到一个小问题:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g. x.new A() where x is an instance of E).
错误提示:没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例。非常郁闷,已经用new实例化了这个类.......。
经过仔细思考发现,我写的内部类是动态的(以public class开头)。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以为了方便起见,最简单的方法:将内部类由 public class
该为 public static class。