溫馨提示×

溫馨提示×

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

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

靜態類不能被繼承(其繼承類不能被實例化,等于不能繼承)

發布時間:2020-08-19 08:35:14 來源:ITPUB博客 閱讀:341 作者:guoyj520 欄目:編程語言

開始聽同事在那討論,有人問靜態類是否能被繼承,我覺得是不能,但沒有找到有說服力的例子,于是寫了一段簡單的代碼來測試,如下:
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為靜態類,bb為其繼承類,類aa能像aa y = new aa();實例化,但類bb卻會出錯,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).
經過多次試驗,均不知道如何實例化類bb,故我的結論是類bb不能實例化。因為靜態類aa的繼承類bb無法實例化,類bb名存實亡,故認為靜態類aa不能被繼承。
(若誰知道如何實例化以上代碼中的類bb,請回復代碼,感謝?。?/p>

[@more@]

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女