// Date:2015/08/16 // test_0816_1.java // 以父類別變數存取子類別物件成員 class Circle{ protected double radius; Circle(double radius){ this.radius = radius; } void show(){ System.out.println("area = " + radius*radius); } } class App extends Circle{ private int value; App(double r, int v){ super(r); value = v; } void show(){ // 要使用 super,才能印出 Circle.show() super.show(); System.out.println("radius = " + radius + ", value = " + value); } } public class test_0816_1{ public static void main(String args[]){ Circle a = new App(0.2,3); a.show(); //發生 override,只會印 App.show() } }
// Date:2015/08/16 // Demo.java // 繼承、多型、轉型練習 class Father{ String name = "Father"; String getName(){ return name; } String greeting(){ return "Class Father"; } } class Son extends Father{ String name = "Son"; String greeting(){ return "Class Son"; } void foo(){ System.out.println(name); //Son System.out.println(this.name); //Son System.out.println(super.name); //Father System.out.println(((Son)this).name); //Son System.out.println(((Father)this).name); //Father System.out.println(((Son)this).greeting()); //Class Son System.out.println(((Father)this).greeting()); //Class Son System.out.println(super.greeting()); //Class Father } } public class Demo{ public static void main(String args[]){ new Son().foo(); } }
首先要先建立 RPG 資料夾,裏面要有(Role.java、Swordman.java、Magician.java、RPG.java)
~$ mkdir RPG ~$ cd RPG/
// Role.java public class Role{ protected String name; protected int level; protected int blood; //name public void setName(String name){ this.name = name; } public String getName(){ return name; } //level public void setLevel(int level){ this.level = level; } public int getLevel(){ return level; } //blood public void setBlood(int blood){ this.blood = blood; } public int getBlood(){ return blood; } public String toString(){ return String.format("(%s, %d, %d)%n", this.name, this.level, this.blood); } public void fight(){} }
// Swordman.java class Swordman extends Role{ public void fight(){ System.out.println("揮劍攻擊"); } }
//Magician.java class Magician extends Role{ public void fight(){ System.out.println("魔法攻擊"); } public void cure(){ System.out.println("魔法治療"); } }
// Date:2015/08/16 // RJG.java(Role.java, Magician.java, Swordman.java) public class RPG{ public static void showBlood(Swordman swordman){ System.out.printf("%s 血量 %d %n", swordman.getName(), swordman.getBlood()); } public static void showBlood(Magician magician){ System.out.printf("%s 血量 %d %n", magician.getName(), magician.getBlood()); } public static void main(String args[]){ Swordman swordMan = new Swordman(); swordMan.setName("Justin"); swordMan.setLevel(1); swordMan.setBlood(200); //System.out.println(swordMan.toString()); System.out.printf("劍士:(%s %d %d)%n", swordMan.getName(), swordMan.getLevel(), swordMan.getBlood()); Magician magician = new Magician(); magician.setName("Monica"); magician.setLevel(1); magician.setBlood(100); //System.out.println(magician.toString()); System.out.printf("魔法師:(%s %d %d)%n", magician.getName(),magician.getLevel(), magician.getBlood()); showBlood(swordMan); showBlood(magician); } }
/*------------------------------------------------------------------------------------------------------*/
.is-a 是一個,是一種上下的關係,利用 extends 來實作延伸類別。
.has-a 有一個,是一種聚合的關係,表示類別的成員變數。
class machine{
}
// is-a 電腦類別繼承電子機械產品類別
class computer extends machine{
CPU theCpu;
RAM theRam;
HD TheHd;
}
/*-----------------------------------------------------------------------------------------------------*/
.interface
介面 介面也是類別的一種,它是用戶端的程式碼,與類別之間的溝通管道, 利用相同的介面可讓程式有一定的規範可循,介面類別完成後就不允許更改。
(主要做用為解決 extends 單一繼承問題)
.宣告
存取權限 interface 介面名稱[]
存取權限 interface 介面名稱 extends //可以讓範圍更大
※
1. 介面的屬性宣告時一定要給定初始值。
2. compile 值,屬性會自動加上 public static final, 方法會自動加上 public abstract。
3. 利用 implements 關鍵字來實作 interface。
4. interface 的方法皆為實作區塊,所以當 implements 時,都一定要將其方法加以實作。
class 類別名稱 implements 介面名稱
/*----------------------------------------------------------------------------------------------------*/
.final 修飾字
- 類別加上 final 不可以被繼承。
- 方法加上 final 不可以被 override。
- 屬性加上 final 不可以放更改。
/*----------------------------------------------------------------------------------------------------*/
// 練習 interface 及 implements interface Pet{ String name="cute"; void move(); void skill(); } public class Dog implements Pet{ // 存取權限一定要大於 interface public void move(){ System.out.println("移動"); } public void skill(){ System.out.println("跑跳"); } public static void main(String args[]){ Dog d = new Dog(); d.skill(); d.move(); } }/*----------------------------------------------------------------------------------------------------*/
※練習 implement 及 extends ,建立檔案:
1. Swimmer.java //interface
2. Flyer.java //interface
3. Driver.java //interface
4. Fish.java //父類別 實作 Swimmer
5. Airplane.java //父類別 實作 Flyer
6. Boat.java //父類別 實作 Swimmer
7. Human.java //父類別
8. Helicopter.java //繼承 Airplane
9. SeaPlan.java //繼承 Airplane 實作 Swimmer
10. Shark.java //繼承 Fish
11. Anemonefish.java //繼承 Fish
12. Piranha.java //繼承 Fish
13. FlyingFish.java //繼承 Fish 實作 Flyer
14. SwimPlayer.java //繼承 Human 實作 Swimmer
15. DemoOcean.java //主程式
首先先建立 Ocean 資料夾,在建立以上檔案。
~$ mkdir Ocean ~$ cd Ocean/
//1. Swimmer.java interface Swimmer{ void swim(); }
//2. Flyer.java interface Flyer{ void fly(); }
//3. Driver.java interface Driver extends Swimmer{ void drive(); }
//4. Fish.java public class Fish implements Swimmer{ protected String name; public Fish(String name){ this.name = name; } public String getName(){ return name; } public void swim(){} }
//5. Airplane.java public class Airplane implements Flyer{ protected String name; Airplane(String name){ this.name = name; } public void fly(){ System.out.printf("飛機 %s 在飛%n", name); } }
//6. Boat.java public class Boat implements Swimmer{ protected String name; Boat(String name){ this.name = name; } public void swim(){ System.out.printf("%s 小船在水面航行%n", name); } }
//7. Human.java public class Human{ protected String name; public Human(String name){ this.name = name; } public String getName(){ return name; } public void swim(){} }
//8. Helicopter.java public class Helicopter extends Airplane{ Helicopter(String name){ super(name); } public void fly(){ System.out.printf("直升機 %s 在飛%n", name); } }
//9. SeaPlan.java public class SeaPlan extends Airplane implements Swimmer{ SeaPlan(String name){ super(name); } public void fly(){ System.out.printf("海上飛機 %s 空中飛行%n", name); } public void swim(){ System.out.printf("海上飛機 %s 水面划行%n", name); } }
//10. Shark.java public class Shark extends Fish{ Shark(String name){ super(name); } public void swim(){ System.out.printf("鯊魚 %s 游泳 %n", name); } }
//11. Anemonefish.java public class Anemonefish extends Fish{ Anemonefish(String name){ super(name); } public void swim(){ System.out.printf("小丑魚 %s 游泳 %n", name); } }
//12. Piranha.java public class Piranha extends Fish{ Piranha(String name){ super(name); } public void swim(){ System.out.printf("食人魚 %s 游泳 %n", name); } }
//13. FlyingFish.java public class FlyingFish extends Fish implements Flyer{ FlyingFish(String name){ super(name); } public void swim(){ System.out.printf("飛魚 %s 游泳%n", name); } public void fly(){ System.out.printf("飛魚 %s 會飛%n",name); } }
//14. SwimPlayer.java public class SwimPlayer extends Human implements Swimmer{ SwimPlayer(String name){ super(name); } public void swim(){ System.out.printf("潛水夫 %s 在游泳%n", name); } }
//15. DemoOcean.java //主程式 public class DemoOcean{ public static void doSwim(Swimmer swimmer){ swimmer.swim(); } public static void doFly(Flyer flyer){ flyer.fly(); } public static void doDriver(Driver driver){ driver.drive(); } public static void main(String args[]){ SeaPlan s = new SeaPlan("S"); FlyingFish f = new FlyingFish("f"); //Swimmer doSwim(new Anemonefish("anemonefish")); doSwim(new Shark("shark")); doSwim(new Piranha("piranha")); doSwim(new SwimPlayer("swimmer")); doSwim(s); doSwim(f); //Flyer doFly(new Helicopter("helicopter")); doFly(s); doFly(f); } }練習 interface method()
// 練習 interface method() // Service.java interface Other{ void execute(); void doOther(); } interface Some{ void execute(); void doSome(); } public class Service implements Some,Other{ public void execute(){ System.out.println("execute"); } public void doSome(){ System.out.println("doSome"); } public void doOther(){ System.out.println("doOther"); } public static void main(String args[]){ Service d = new Service(); d.execute(); d.doSome(); d.doOther(); } }
沒有留言:
張貼留言