= Java的 類別、抽象、介面 = == 類別 == * 類別是實體的,因此他可以繼承(extends),並且可以用new 來產生 * 由以下範例可得知Father whoami = new Son(); 這樣產生出的物件,其實還是Father物件,可想做 (Father)(new Son())父類別的強制轉型 {{{ #!java class Father { String Iam = "father"; } class Son extends Father { String Iam = "son"; } public class test { Father whoami = new Son(); System.out.println ( whoami.iam ); } }}} {{{ father }}} == 介面 == * 介面為需要實做的類別,因此無法用new產生,且無法用extends繼承 * 產生介面唯一能用的是implement在指定的類別上 * implement 可以多重繼承 ,也可以介面implement介面 * 一旦繼承了,就一定要實做內部的功能 * interface的成員可看成是 '''public static final''',因此方法不可以有內容,而變數卻一定要給初值 * 需要被實做的功能,一定有一個特性,就是這個功能會有帶入的參數,於是我們就可以使用這些參數來進行實做 * 以下實做了圖形介面的按鍵,由於implements ActioinListener,因此要實做actionPerformed功能,而這功能可以用他的event來進行實做 {{{ #!java class A extends Applet Implements ActionListener { ... void actionPerformed(ActionEvent event){ event.getSource(); ... } } }}} == 抽象類別 == * abstract 為抽象類別:當您定義類別時,可以僅宣告方法名稱而不實作當中的邏輯,這樣的方法稱之為「抽象方法」(Abstract method),如果一個類別中包括了抽象方法,則該類別稱之為「抽象類別」(Abstract class),抽象類別是個未定義完全的類別,所以它不能被用來生成物件,它只能被擴充,並於擴充後完成未完成的抽象方法定義。 * extends 繼承來擴充之,一旦將繼承來的abstract方法都實做完成,此類別就可以被繼承 * 因此如果我的類別裡,有用到abstract 的method,就是abstract類別 * 回到第一個範例,Father whoami = new Son(); 感覺很無聊,怎麼不Father whoami = new Son(); 就好了,還要惡搞Father & Son ,難道只是用來出考題的? * 其實如果Father為抽象類別,Son繼承Father並為實做完後的類別,則Son whoami = new Son();當然ok,但若想要僅用Father的參數,只是透過Son以 new出來(Father為抽象類別無法用new),則Father whoami = new Son(); 就很實用了 == 釐清 抽象 與 介面 == {{{ #!java abstract class AC { AC(){ } // abstract would have constructor abstract void method1(); abstract void method2(); } interface INF { // INF(){ } // error : interface cannot have constructor void method1(); void method2(); } public class test { public static void main(String[] args) { // INF if_err1 = new INF; // error : INF cannot be resolved // INF if_err2 = new INF(); // error: Cannot instantiate the type INF INF[] inf = new INF[5]; // declare as array would be work // AC ac_err1 = new AC; // error : AC cannot be resolved // AC ac_err2 = new AC(); // even AC have construstor but still error: Cannot instantiate the type AC AC[] ac = new AC[5]; // declare as array would be work } } }}} * abstract :Demo可以有自己的數據成員,也可以有非abstarct的成員方法, * interface:Demo只能夠有靜態的不能被修改的數據成員,所有的成員方法不能有內容。 * 從某種意義上說,interface是一種特殊形式的abstract class。 == 範例 == {{{ #!java interface CanFight { void fight(); } interface CanSwim { void swim(); } interface CanFly { void fly(); } interface CanClimb { void climb(); } class ActionCharacter { public void fight() {} } class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly, CanClimb { public void swim() {} public void fly() {} public void climb() {} } public class Ex09 { static void t(CanFight x) { x.fight(); } static void u(CanSwim x) { x.swim(); } static void v(CanFly x) { x.fly(); } static void z(CanClimb x) { x.climb(); } static void w(ActionCharacter x) { x.fight(); } public static void main(String[] args) { Hero h = new Hero(); t(h); // Treat it as a CanFight u(h); // Treat it as a CanSwim v(h); // Treat it as a CanFly z(h); // Treat it as a CanClimb w(h); // Treat it as an ActionCharacter } } }}}