日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing i...

發布時間:2024/3/24 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing i... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在刷AC,在編譯時出現:

No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).

根據提示,沒有可訪問的內部類的實例,必須分配一個合適的內部類的實例(如x.new A(),x必須是內部類的實例。看著這句提示,我。。。

我已經用new實例化了這個類,為什么還不行呢。

于是仔細看了一下IDE提示的地方,看了一下代碼,突然發現,一般AC題目的時候,要求main方法是static的,而我寫的Student內部類是非靜態的,所以無法訪問

最簡單的改法:將Student改為靜態方法之后,不再報錯。
這個錯誤很低級,將其總結,提醒自己,之后不可再錯。

此處,我們引出另外一個知識點,Java靜態方法訪問非靜態方法的實現方法有哪些?
不多說,直接上代碼:

1) 將內部類修改,變為外部

class test{ public static void main(String args[]){ System.out.println(new test1().methodname()); } } class test1{ public String methodname(){ System.out.println("調用了非靜態方法"); } }

2) 將內部類修改為靜態內部類

class test{ public static void main(String args[]){ System.out.println(new test1().methodname()); } public class static test1{ public String methodname(){ System.out.println("調用了非靜態方法"); } } }

總結

以上是生活随笔為你收集整理的No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing i...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。