看板 java 關於我們 聯絡資訊
剛接觸java,還在摸索中請多多包含 參考網頁:http://programming.im.ncnu.edu.tw/J_index.html 內容: class Animal { private String moveMethod() { return "Unspecified"; } public static void main(String[] argv) { Animal a1; a1 = new Bird(); System.out.println(a1.moveMethod()); // print out "Unspecified" } } class Bird extends Animal { // this is not override because Bird can't see Animal's moveMethod public String moveMethod() { return "Fly"; } } 這邊在講的是override中static不能被覆寫的觀念,我想問的是 Animal a1 = new Bird(); 跟 Bird a1 = new Bird(); 差別在哪? 亂猜: 跟〝父類別class是abstruct的話一定要靠子類別去override才能new物件〞有關嗎? ========================================================== 因為我是自學沒有老師可以問,如果問題太笨不要見笑 > < -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.229.129.83
coolsprite:你的Animal不是abstruct吧 06/06 12:39
conan77420:嗯不是,但我只是想在其他例子下是不是這可能 06/06 12:42
coolsprite:如果子類別看不到父類別的方法(如父類別的private方法, 06/06 13:18
coolsprite:而子類別定義了父類別內的package method),則就算定義 06/06 13:19
coolsprite:了同樣的method,也不是override (你給的網站上寫的) 06/06 13:21
Puser:上面那個現在是Animal 現在只能存取a的欄位方法 不能存取b的 06/06 14:07
conan77420:如果Animal的不是static,而我打bird a1=new bird()的 06/06 15:00
conan77420:話,跟打Animal a1=new bird()有何不同? 06/06 15:02
Puser:Animal a1=new bird() al被向上轉型(cast)成Animal 測看就知 06/06 15:33
Puser:不知道為什麼不能向下鑄型 要請強者大大出來分說@@" 06/06 15:35
Kelunyang:Bird b1 = (bird)a1 為什麼不行@@? 06/07 18:14