book.java
public class Book {
private String name; public Book(String name) {
this.name = name;
} public static Book random() {
int randomNumber = (int) (Math.random() * 5 + 1);
System.out.println("Random number: " + randomNumber);
switch (randomNumber) {
case 1:
return new MeanGenes();
case 2:
return new GettingThingsDone();
case 3:
return new Influence();
case 4:
return new ASeventhChildAndTheLaw();
case 5:
return new FooledByRandomness();
}
return null;
} public String author() {
return "No find author";
} public String getName() {
return name;
}
}
class MeanGenes extends Book {
public MeanGenes() {
super("MeanGenes");
} @Override
public String author() {
return "Terence C. Burnham";
}
}class GettingThingsDone extends Book {
public GettingThingsDone() {
super("GettingThingsDone");
} @Override
public String author() {
return "David Allen";
}
}class Influence extends Book {
public Influence() {
super("Influence");
} @Override
public String author() {
return "Robert Cialdini";
}
}class ASeventhChildAndTheLaw extends Book {
public ASeventhChildAndTheLaw() {
super("ASeventhChildAndTheLaw");
} @Override
public String author() {
return "Patrick Yu";
}
}class FooledByRandomness extends Book {
public FooledByRandomness() {
super("FooledByRandomness");
}
}
main.java
public class main { public static void main(String[] args) { for (int i = 1; i <= 10; i++) {
Book book = Book.random();
System.out.println("Book name: " + book.getName() + "\n"
+ "Author: " + book.author() + "\n");
}
}
}
輸出結果:
Random number: 1
Book name: MeanGenes
Author: Terence C. Burnham
Random number: 4
Book name: ASeventhChildAndTheLaw
Author: Patrick Yu
Random number: 1
Book name: MeanGenes
Author: Terence C. Burnham
Random number: 4
Book name: ASeventhChildAndTheLaw
Author: Patrick Yu
Random number: 5
Book name: FooledByRandomness
Author: No find author
Random number: 4
Book name: ASeventhChildAndTheLaw
Author: Patrick Yu
Random number: 1
Book name: MeanGenes
Author: Terence C. Burnham
Random number: 2
Book name: GettingThingsDone
Author: David Allen
Random number: 1
Book name: MeanGenes
Author: Terence C. Burnham
Random number: 4
Book name: ASeventhChildAndTheLaw
Author: Patrick Yu
能看到第5本書 FooledByRandomness 沒有寫入作者 author()
系統自動調用了繼承的父類
但這個案例也把我搞糊塗了
這不就是繼承嗎
跟 Polymorphism 有啥關係
我能看出來的好處就是
for 能方便的循環
且創立時用的不是自己的 Class
小挑戰
Create a base class called Car
It should have a few fields that would be appropriate for a generice car calss.
engine, cylinders, wheels, etc.
Constructor should initialize cylinders (number of) and name, and set wheels to 4
and engine to true. Cylinders and names would be passed parameters.
Create appropriate getters
Create some methods like startEngine, accelerate, and brake
show a message for each in the base class
Now create 3 sub classes for your favorite vehicles.
Override the appropriate methods to demonstrate polymorphism in use.
put all classes in the one java file (this one).
小挑戰參考答案
class Car {
private String name;
private boolean engine;
private int cylinders;
private int wheels; public Car(String name, int cylinders) {
this.name = name;
this.cylinders = cylinders;
wheels = 4;
engine = true;
} public void startEngine(){
System.out.println("Start Engine from Car");
} public void acceleration(){
System.out.println("acceleration from Car");
} public void brake(){
System.out.println("brake from Car");
} public String getName() {
return name;
}
public int getCylinders() {
return cylinders;
}}class Benz extends Car {
public Benz(String name, int cylinders) {
super(name, cylinders);
} @Override
public void startEngine() {
System.out.println("Start Engine from Benz");
} @Override
public void acceleration() {
System.out.println("acceleration from Benz");
} @Override
public void brake() {
System.out.println("brake from Benz");
}}class BMW extends Car {
public BMW(String name, int cylinders) {
super(name, cylinders);
}
@Override
public void startEngine() {
System.out.println("Start Engine from BMW");
} @Override
public void acceleration() {
System.out.println("acceleration from BMW");
} @Override
public void brake() {
System.out.println("brake from BMW");
}
}class Audi extends Car {
public Audi(String name, int cylinders) {
super(name, cylinders);
}
@Override
public void startEngine() {
System.out.println("Start Engine from Audi");
} @Override
public void acceleration() {
System.out.println("acceleration from Audi");
} @Override
public void brake() {
System.out.println("brake from Audi");
}
}
public class Car_Polymorphism {
public static void main(String[] args) {
Car benz1 = new Benz("M6", 4);
benz1.startEngine();
benz1.acceleration();
benz1.brake();
System.out.println(); Benz benz = new Benz("M6", 4);
benz.startEngine();
benz.acceleration();
benz.brake();
System.out.println();
}}
輸出結果:
Start Engine from Benz
acceleration from Benz
brake from Car
Start Engine from Benz
acceleration from Benz
brake from Car
我好像看出點端倪了
Car benz = new Benz("M6", 4);
Benz benz = new Benz("M6", 4);
如果不像答案的代碼 寫在同一個地方
第一個 benz 不用改名
然而根據使用情況
兩個都能成立
差別就在
第一個是屬於 Car Class 的 benz object
第一個是屬於 Benz Class 的 benz object
Tim 講師的 QA 補充
The difference is ford in the first case is of type Car and the second is of type Ford.
So the first statement requires more work to get the methods that relate to the Ford class to be executed.
But the second example does not.
Here is an example.
class Car {
private String name;
public Car(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Ford extends Car {
private int gears;
public Ford(String name, int gears) {
super(name);
this.gears = gears;
}
public int getGears() {
return gears;
}
}
public class Main {
public static void main(String[] args) {
Car f1 = new Ford("Ford",2);
Ford f2 = new Ford("F2", 5);
// Valid
f1.getName();
// Won't work
f1.getGears();
// Valid
f2.getName();
// Works
f2.getGears();
}
}
Note hoe the “Car” definition does not allow you to access the gears getter, but the “Ford” definition does.
在補充代碼中能看到 f1.getGears()
是會報錯的 在 Car 中是沒有這個 method 的
在子類 Ford 才有此 method