一、錯誤提示
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 即可。