日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/3/24 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing i... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近在刷AC,在編譯時(shí)出現(xiàn):

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).

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

我已經(jīng)用new實(shí)例化了這個(gè)類,為什么還不行呢。

于是仔細(xì)看了一下IDE提示的地方,看了一下代碼,突然發(fā)現(xiàn),一般AC題目的時(shí)候,要求main方法是static的,而我寫的Student內(nèi)部類是非靜態(tài)的,所以無法訪問

最簡(jiǎn)單的改法:將Student改為靜態(tài)方法之后,不再報(bào)錯(cuò)。
這個(gè)錯(cuò)誤很低級(jí),將其總結(jié),提醒自己,之后不可再錯(cuò)。

此處,我們引出另外一個(gè)知識(shí)點(diǎn),Java靜態(tài)方法訪問非靜態(tài)方法的實(shí)現(xiàn)方法有哪些?
不多說,直接上代碼:

1) 將內(nèi)部類修改,變?yōu)橥獠?/strong>

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

2) 將內(nèi)部類修改為靜態(tài)內(nèi)部類

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。