Java 2013.03.06
Outline Inheritance Superclasses and Subclasses Using super keyword Overiding Method Overiding vs. Overloading Dynamic Binding Polymorphism [Sample code] Novice.java、Magician.java 、Swordman.java 、Game.java DynamicBindingDemo.java
Inheritance Object-oriented programming allows you to derive new classes from existing classes. This is called inheritance.
Superclasses and Subclasses Novice HP MP attack() inheritance inheritance Magician Swordman magic_attack() power_attack()
程式範例: Novice.java /* Superclass */ public class Novice //Novice類別 { //---state, attribute----- int HP = 100; int MP = 50; //---constructor---------- Novice() {} //---behavior------ void attack() //代表attack行為的method System.out.println("Attack!"); }
程式範例: Magician.java /* subclass */ public class Magician extends Novice //Magician類別 繼承Person { //---state, attribute----- //---constructor---------- Magician() this.HP = 100; this.MP = 250; } //---behavior------ void magic_attack() //代表magic attack行為的method if(this.MP >=10) this.MP -= 10; System.out.println("Magic Attack!");
程式範例: Swordman.java /* Subclass */ public class Swordman extends Novice // Swordman類別 { //---state, attribute----- //---constructor---------- Swordman() this.HP = 300; this.MP = 50; } //---behavior------ void power_attack() //代表attack行為的method System.out.println("Power Attack!");
程式範例: Game.java /* Game */ public class Game { public static void main(String[] args) // Declair a Novice object Novice player1 = new Novice(); System.out.println("player1 -> HP:" + player1.HP + " MP:" + player1.MP); player1.attack(); // Declair a Magician object Magician player2 = new Magician(); System.out.println("player2 -> HP:" + player2.HP + " MP:" + player2.MP); player2.attack(); player2.magic_attack(); // Declair a Swordman object Swordman player3 = new Swordman(); System.out.println("player3 -> HP:" + player3.HP + " MP:" + player3.MP); player3.attack(); player3.power_attack(); // Declair a Archer object Archer player4 = new Archer(); System.out.println("player3 -> HP:" + player4.HP + " MP:" + player4.MP); player4.attack(); }
Using super keyword The super refers to the superclass of the class in which super appears. It can be used in two ways: 1. To call a superclass constructor. 2. To call a superclass method.
Overiding Method Novice HP MP attack() inheritance Archer attack() overriding
程式範例: Archer.java /* Subclass */ public class Archer extends Novice // Archer類別 { //---state, attribute----- //---constructor---------- Archer() this.HP = 200; this.MP = 150; } //---behavior------ void attack() //代表attack行為的method, override the Novice.attack() System.out.println("Shoot!");
Overiding vs. Overloading Overriding 覆寫 Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. Overloading 多載 Define multiple methods with the same name but different signature . (課本 p.407)
Dynamic Binding Dynamic Binding 動態載入 物件的行為並不是在編譯時期 (compiler-time)就已經 決定了。而是在程式執行時期於(run-time)才動態地決定 的。如何動態地決定。就看物件當時的狀態(state)而定, 物件封裝了所有可能的狀態處理 方法,並且根據外邊送 來的訊息做出適當的反應。 (課本 p.409)
程式範例: DynamicBindingDemo.java public class DynamicBindingDemo { public static void main(String [] args) m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Object x) System.out.println(x.toString()); class GraduateStudent extends Student class Student extends Person public String toString() return "Student"; class Person return "Person";
Polymorphism Polymorphism 多型 簡單來說,多型是指一個物件可以擁有多個不同的型 態,而這些型態必須是該物件所繼承的其他類別。
生物 動物 人 Organism O = new Organism(); People P = new People(); Organism O2 = new People(); 生物 extends 動物 extends 人
- Overriding - Overloading - Dynamic binding - Interface/abstract Polymorphism - Overriding - Overloading - Dynamic binding - Interface/abstract
ArrayList ArrayList class that can be used to store an unlimited number of objects. Java.util.ArrayList add(o object): void add(index: int, o: object): void clear(): void indexOf(o: object): int isEmpty(): boolean lastIndexOf(o: object): int remove(o: obect): boolean size(): int
程式範例: TestArrayList.java /* ArrayList */ class Car { int speed() return 100; } void run() System.out.println("Runing!"); public class TestArrayList @SuppressWarnings("unchecked") public static void main(String[] args) java.util.ArrayList cityList = new java.util.ArrayList(); cityList.add("Taipei"); cityList.add("Taichung"); cityList.add("Kaushiung"); java.util.ArrayList list = new java.util.ArrayList(); list.add(new Car()); System.out.println(((Car)list.get(0)).speed()); //System.out.println(((Car)list.get(1)).run());
practice 1. (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person’s name.
Ppt下載: http://oss.csie.fju.edu.tw/~jastine01/ppt.html