溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

靜態(tài)類不能被繼承(其繼承類不能被實(shí)例化,等于不能繼承)

發(fā)布時(shí)間:2020-08-19 08:35:14 來源:ITPUB博客 閱讀:335 作者:guoyj520 欄目:編程語言

開始聽同事在那討論,有人問靜態(tài)類是否能被繼承,我覺得是不能,但沒有找到有說服力的例子,于是寫了一段簡單的代碼來測試,如下:
public class staticTest {
static class aa{
void abc(){
System.out.println("1111111");
}
}

class bb extends aa{
void abc(){
System.out.println("222222");
}
}

public static void main(String[] args) {
//bb x = new bb();
aa y = new aa();
y.abc();
}
}
其中aa為靜態(tài)類,bb為其繼承類,類aa能像aa y = new aa();實(shí)例化,但類bb卻會(huì)出錯(cuò),eclipse中提示為No enclosing instance of type staticTest is accessible. Must qualify the allocation with an enclosing instance of type staticTest (e.g. x.new A() where x is an instance of staticTest).
經(jīng)過多次試驗(yàn),均不知道如何實(shí)例化類bb,故我的結(jié)論是類bb不能實(shí)例化。因?yàn)殪o態(tài)類aa的繼承類bb無法實(shí)例化,類bb名存實(shí)亡,故認(rèn)為靜態(tài)類aa不能被繼承。
(若誰知道如何實(shí)例化以上代碼中的類bb,請(qǐng)回復(fù)代碼,感謝?。?/p>

[@more@]

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI