2013.03.06. inheritance superclasses and subclasses using super keyword overiding method overiding...
TRANSCRIPT
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
HPMP
magic_attack() power_attack()
Novice
Magician Swordman
attack()
inheritance inheritance
程式範例 : 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 objectNovice player1 = new Novice();System.out.println("player1 -> HP:" + player1.HP + " MP:" + player1.MP);player1.attack();
// Declair a Magician objectMagician player2 = new Magician();System.out.println("player2 -> HP:" + player2.HP + " MP:" + player2.MP);player2.attack();player2.magic_attack();
// Declair a Swordman objectSwordman player3 = new Swordman();System.out.println("player3 -> HP:" + player3.HP + " MP:" + player3.MP);player3.attack();player3.power_attack();
// Declair a Archer objectArcher 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.
程式範例 : 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{
public String toString(){
return "Person";}
}
Organism O = new Organism();
People P = new People();
Organism O2 = new People();
生物
動物
人
extends
extends
ARRAYLIST ArrayList class that can be used to store an unlimited number
of objects.
add(o object): voidadd(index: int, o: object): voidclear(): voidindexOf(o: object): intisEmpty(): booleanlastIndexOf(o: object): intremove(o: obect): booleansize(): int
Java.util.ArrayList
程式範例 : 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());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.