Course Introduction CLASS II
1-2 Previous studies What is constructor? How to use this()? –Where I can call this()? –Can I call this() twice in one constructor? –What is the meaning between this() and this.XX?
1-3 Previous studies How to use Inheritance? How to use super()? –Where can I call super()? –What is the meaning between super() and super.XX? What is polymorphism?
Synopsis of Polymorphism The polymorphism principle Polymorphism means that an operation may behave differently (in different classes). There are two kinds of polymorphism –static (overloading) – dynamic Example –GeomFigure (display(), remove(), position()) Rectangle Circle 1-4
再談 inheritance 矩型 – 我們先擁有這個 class ,並且進行了系統開發 正方形 – 之後有了正方型的需求,再寫了一個正方形的 class –Square “IS A” kind of rectangle Reuse? –Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? –Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? 1-5
Class Rectangle class Rectangle { double width; double height; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } } 1-6
Class Square class Square extends Rectangle { public void setHeight(double height) { super.setHeight(height); super.setWidth(height); } public void setWidth(double width) { super.setHeight(width); super.setWidth(width); } } 1-7
void g(Rectangle r) { r.setWidth(5); r.setHeight(4); if (r.getWidth() * r.getHeight() != 20) { throw new RuntimeException(); } } //some other place Rectangle square = new Square(); g(square); 1-8
Liskov Substitution Principle (LSP) Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it Very difficult problem!! –Further study: OOAD –Software design principals Please google it if you are interested in it One of the principals –Prefer Composition over inheritnace –Why? Can you give an example? 1-9
1-10 類別( Class )架構 「類別架構」( Class Hierarchy ) – 將整個類別關係的樹狀結構繪出來 – 繼承的子類別可以有多層 如果父類別不只一個,即繼承多個類別,稱為 「多重繼承」( Multiple Inheritance )。
1-11 類別( Class )類別關係 類別關係是指不同類別間的關係,例如: – 繼承是一種 Is-a 的類別關係 Nissan” 是 ” 車子的一種 (Is-a) – 「成品和零件」( Whole-Part )關係,即 Part-of 和 Has-a 關係。 輪胎是車子的 ” 一部份 ”(Part-of) 車子 ” 有 ” 輪胎 (Has-a)
1-12 「 is a 」和「 has a 」 在描述類別時,常以「 is a 」和「 has a 」描述類別的父類 別和類別中的資料成員。「 is a 」描述句通常會導出類別 的父類別,而「 has a 」則是會導出類別中的資料成員。 –A man is a human that has a name, a father, and a mother. public class Man extends Human { Name theName; Father theFather; Mother theMother; }
1-13 Wrong program // 建立一個 Vehicle 的類別 class Vehicle { Vehicle(String x){ System.out.println(“Vehicle’s Constructor”); } public void drive(){ System.out.println(“I’m driving”); }
1-14 Wrong program // 建立一個 Car 的類別 extend Vechicle class Car extends Vehicle { } public class app{ public static void main(String[] args){ Car aCar=new Car(); } Why this program is wrong??
1-15 How to fix it! // 建立一個 Vehicle 的類別 class Vehicle { Vehicle(){ System.out.println(“Vehicle’s Constructor”); } Vehicle(String x){ System.out.println(“Vehicle’s Constructor”); } public void drive(){ System.out.println(“I’m driving”); }
1-16 How to fix it!! // 建立一個 Car 的類別 extend Vechicle class Car extends Vehicle { Car(){ Super(“X”); } public class app{ public static void main(String[] args){ Car aCar=new Car(); }
1-17 How to fix it!! // 建立一個 Car 的類別 extend Vechicle class Car extends Vehicle { Car(String x){ Super(x); } public class app{ public static void main(String[] args){ Car aCar=new Car(“x”); }
1-18 Example class Circle{ double radius; void setRadius(double radius){ this.radius=radius; } void showRadius(){ System.out.println(“ 半徑為 ”+radius); } double showArea(){ return Math.PI*Math.pow(radius,2); }
1-19 API search Search java api document For example pow(a,b) –Key in java api pow
1-20 Example public class Application{ public static void main(String argv[]){ Circle aCircle; aCircle = new Circle(); aCircle.setRadius(12.0); aCircle.showRadius(); System.out.println(“aCircle 的面積等於 ” + aCircle.showArea()); }
1-21 How to know new object’s hashCode? public class RealWorld { public static void main(String[] args){ Car aCar=new Car();// 產生一個 Car 的實體 String x=aCar.toString(); System.out.println(x); Car bCar=aCar; System.out.println(aCar.toString()); System.out.println(bCar.toString()); }
1-22 Example detail Circle aCircle; aCircle = new Circle(); aCircle 的記憶體 location l-value:0x1111 指向 Circle Type 的 reference 的 variable aCircle l-value:0x1111 Circle 某一個 instance 其 l-value:0x3333 aCircle = new Circle(); aCircle l-value:0x1111 aCircle r-value:0x3333 Circle 某一個 instance 其 l-value:0x3333
1-23 Example detail bCircle=aCircle; Circle 物件 aCircle bCircle bCircle=null; Circle 物件 aCircle bCircle Problem: aCircle=null; 結果會如何 ?
1-24 物件的空間配置 class Vehicle{// 定義的類別 int wheel; // 定義一個實體變數 } Vehicle newCar1 = new Vehicle();// 實體化一個類別 Vehicle newCar2 = new Vehicle();// 再實體化一個類別 Vehicle rCar = newCar1;// 只是一個參考
1-25 變數型態 種類 – 基本型態 (Primitive Type) 8 種 – 參考型態 (Reference Type) 基本型態 – 整數、浮點數、字元、或布林值 參考型態 – 由物件所組成的型態 So, what is “static”? – We have not explained it, let us do it now.