開始聽同事在那討論,有人問靜態類是否能被繼承,我覺得是不能,但沒有找到有說服力的例子,于是寫了一段簡單的代碼來測試,如下:
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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。