第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

把Class.forName()換一種寫法就報(bào)錯(cuò)了!

1?首先,這段代碼沒有任何問題。
public?class?OfficeBetter{
??????public?static?void?main(String[]?args){
??????try{
????????OfficeAble?oa?=(OfficeAble)?Class.forName(args[0]).getConstructor().newInstance();
????????oa.start();
??????}catch(Exception?e){
e.printStackTrace();
??????}
??}
}

2 可是,當(dāng)我不想用

Class.forName(args[0])

來獲取類類型,改成這種寫法就報(bào)錯(cuò)了:

public?class?OfficeBetter{
??????public?static?void?main(String[]?args){
??????try{
????????Class?c=Class.forName(args[0]);
????????Constructor?constructor=c.getConstructor();
????????OfficeAble?oa=(OfficeAble)constructor.newInstance();
????????oa.start();
??????}catch(Exception?e){
e.printStackTrace();
??????}
??}
}

誰能幫我分析一下嗎?編譯報(bào)錯(cuò):

OfficeBetter.java:5:?error:?cannot?find?symbol
????????Constructor?constructor=c.getConstructor();
????????^
??symbol:???class?Constructor
??location:?class?OfficeBetter
Note:
OfficeBetter.java?uses?unchecked?or?unsafe?operations.
Note:?Recompile?with?-Xlint:unchecked?for?details.
1?error


正在回答

2 回答

?本來代碼里面是動(dòng)態(tài)類實(shí)現(xiàn),你Constructor?constructor=c.getConstructor();這行代碼使用了靜態(tài)類的實(shí)現(xiàn)方式,應(yīng)該會(huì)編譯不過,因?yàn)閏.getConstructor()無法在靜態(tài)編譯期間確定類型。

1 回復(fù) 有任何疑惑可以回復(fù)我~

正解。多謝你的解答,后面我看書之后,了解了Class.forName("xxx")是動(dòng)態(tài)編譯。當(dāng)時(shí)不懂。下面是我的筆記,

http://img1.sycdn.imooc.com//5f0c138200010f6316931079.jpg

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

把Class.forName()換一種寫法就報(bào)錯(cuò)了!

我要回答 關(guān)注問題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)