一、错误提示
No enclosing instance of type ImageViewer is accessible.
Must qualify the allocation with an enclosing instance of type ImageViewer (e.g.x.new A() where x is an instance of ImageViewer).
ImageViewer是内部类实例
二、分析
没有访问的内部类实例,必须分配一个合适的内部类的实例(如x.new A(),x必须是ImageViewer的实例) 尴尬的我发现我已经写了啊,但是还是报错,为何不行呢?
由于写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main.在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中用该类的成员变量与成员方法。
三、解决
将public class 改为public static class 即可。