National Taiwan University Department of Computer Science and

  • Slides: 50
Download presentation
National Taiwan University Department of Computer Science and Information Engineering Managing Inheritance Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Managing Inheritance Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Outline 存取控制(Access Control) static

National Taiwan University Department of Computer Science and Information Engineering Outline 存取控制(Access Control) static 關鍵字 實體成員與類別成員 static 初值設定區塊 Your Turn 物件導向語言三大特性 封裝 (Encapsulation) 繼承(Inheritance) 同名異型( Polymorphism ) • Overriding • Overloading super 關鍵字 Java 物件祖先:Object 類別 Final Classes and Methods Your Turn

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control) public 任何類別皆可存取

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control) public 任何類別皆可存取 default packetage access protected 允許宣告的類別、子類別與同一個套件中的類別使用 private 只有在類別內部可以存取 對於成員變數,通常設定為 private 權限,再透過 methods, 如 get/set 來存取資料

National Taiwan University Department of Computer Science and Information Engineering Package與示範存取的範例 用eclipse功能 new->package, 建立Package

National Taiwan University Department of Computer Science and Information Engineering Package與示範存取的範例 用eclipse功能 new->package, 建立Package “One” 在One裡面新增類別 “Alpha” Alpha類別只是一個 單純的物件類別 在One裡面新增類別 “Delta. One” Delta. One類別用同Package 的Alpha類別建立物件 新增Package “Two” 新增類別“Delta Two” Delta. Two類別用別Package 的Alpha類別建立物件

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control)範例 Example: Alpha.

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control)範例 Example: Alpha. java private int iamprivate = 1; int iampackage = 2; //package access protected int iamprotected = 3; public int iampublic = 4; private void private. Method() { System. out. println("iamprivate Method"); } void package. Method() { // default: package access System. out. println("iampackage Method"); } protected void protected. Method() { System. out. println("iamprotected Method"); } public void public. Method() { System. out. println("iampublic Method"); }

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control)範例 Example: Delta.

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control)範例 Example: Delta. One. java package One; public class Delta. One { public static void main(String[] args) { Alpha a = new Alpha(); //a. private. Method(); //illegal a. package. Method(); //legal a. protected. Method(); //legal a. public. Method(); //legal //System. out. println("iamprivate: “ + a. iamprivate); //illegal System. out. println("iampackage: “ + a. iampackage); //legal System. out. println("iamprotected: “ + a. iamprotected); //legal System. out. println("iampublic: “ + a. iampublic); //legal } }

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control)範例 Example: Delta.

National Taiwan University Department of Computer Science and Information Engineering 存取控制(Access Control)範例 Example: Delta. Two. java package Two; import One. *; public class Delta. Two { public static void main(String[] args) { Alpha alpha = new Alpha(); //alpha. private. Method(); //illegal //alpha. package. Method(); //illegal //alpha. protected. Method(); //illegal alpha. public. Method(); //legal //System. out. println("iamprivate: “ + alpha. iamprivate); //illegal //System. out. println("iampackage: “ + alpha. iampackage); //illegal //System. out. println("iamprotected: “ + alpha. iamprotected); //illegal System. out. println("iampublic: “ + alpha. iampublic); //legal } }

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 實體成員與類別成員 變數或

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 實體成員與類別成員 變數或 methods 若宣告為 static,則此變數或 methods 即 為類別變數(class variables)或類別方法(class methods) 宣告為 static 的變數或 methods 不屬於任何此類別的物件, 乃此類別所有物件共同擁有 宣告 • static Data. Type Var. Name; • static Return. Type Method. Name(Arg List) 使用 • Class. Name. Var. Name • Class. Name. Method. Name(Arg List)

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 使用時機 無論此類別擁有多少物件,這份資料只需要一份

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 使用時機 無論此類別擁有多少物件,這份資料只需要一份 某種方法在實行時,與個別的物件無關 即便是沒有任何物件被產生,依舊可以使用被宣告 成 static 的變數與 methods;相反的,instance variables 與 methods 必須透過物件才能實施 在 static methods 裡,無法直接呼叫 instance methods 或直接使用 instance variables 因為 instance variables 與 methods 必須透過物件才能實施

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 被用 static

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 被用 static 宣告的類別方法和類別變數,意思上就 像是其他程式語言中的全域函數(global functions) 和全域變數(global variables),例如:C語言 因此,如果要很多宣告成 static 的方法或變數的話, 必須要謹慎使用!

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字- 實體成員與類別成員

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字- 實體成員與類別成員

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 - 實體成員與類別成員

National Taiwan University Department of Computer Science and Information Engineering static 關鍵字 - 實體成員與類別成員 Example: Static. Demo. java public class Static. Demo { public int instance. Integer = 0; public int instance. Method() { return instance. Integer; } public static int class. Integer = 0; public static int class. Method() { return class. Integer; } public static void main(String[] args) { … } }

National Taiwan University Department of Computer Science and Information Engineering static 初值設定區塊 class 變數或是實體變數,可以直接設定初值

National Taiwan University Department of Computer Science and Information Engineering static 初值設定區塊 class 變數或是實體變數,可以直接設定初值 public class Bed. And. Breakfast { public static final int MAX_CAPACITY = 10; //initialize to 10 private boolean full = false; //initialize to false } 限制: 不能用 if-else 來設定初值時,不可以處理 exceptions 若發生 exceptions,也無法做錯誤處理

National Taiwan University Department of Computer Science and Information Engineering static 初值設定區塊 Java 允許將

National Taiwan University Department of Computer Science and Information Engineering static 初值設定區塊 Java 允許將 static 變數集合起來進行初始化動作 語法: class Test. Class{ static int x; static boolean is. OK; static{ x = 100; //static int x; is. OK = false; // static is. OK; } } 因為 x 和 is. OK 都是 class 變數,無法在建構元中設定初 值,且利用 static 初設設定區塊可以處理 exceptions。

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint //

National Taiwan University Department of Computer Science and Information Engineering Your Turn Hint // 建構元 private Rectangle(){ … } // 另一個建構元 private Rectangle(int length, int width){ … } // 此建構元提供給 clone. Obj 用,用以複製 private Rectangle(Rectangle obj){ … } // 提供一個可以製造長方形的 class method static Rectangle create. Rect() { … } // 提供一個可以製造且自行設定長方形的 class method static Rectangle create. Rect(int len, int width) { … } // 複製此長方形 static Rectangle clone. Rect(Rectangle obj) { … } // 顯示現在的長、寬 void show. LW() { … } // 取得現在的面積 double get. Area(){ … } // 畫出此長方形 void draw. Rect(){ … }

National Taiwan University Department of Computer Science and Information Engineering 封裝 (Encapsulation) 將資料(屬性)與方法(行為)封裝在一個物件裡頭,物件 裡頭的資料與方法被緊緊的綁在一起,並擁有資訊隱藏(

National Taiwan University Department of Computer Science and Information Engineering 封裝 (Encapsulation) 將資料(屬性)與方法(行為)封裝在一個物件裡頭,物件 裡頭的資料與方法被緊緊的綁在一起,並擁有資訊隱藏( Information hiding)的特性。 Example: Time. Demo. java class Time { private int hour; private int minute; private int second; public Time() { … } public void set. Time(int hh, int mm, int ss) { … } public String to. String() { … } }

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) 繼承概念圖

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) 繼承概念圖

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) 語法: class Class.

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) 語法: class Class. Name extends Base. Class 例如:class Line extends Graphics. Object Base Class(Super. Class):基底類別、父類別 Derived Class(Subclass):衍生類別、子類別 Java 理論上不支援多重繼承,也就是說,一個子類別只能有一個父 類別。 子類別將會繼承到父類別中所有可以存取的成員,包括:變數以及 方法 注意:建構元(constructor)無法被繼承 Java 中每個物件的總祖先:Object 類別( java. lang. Object )

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) 可繼承成員 Superclass 中宣告為

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) 可繼承成員 Superclass 中宣告為 public 或 protected 的成員。 如果 Subclass 與 Superclass 在同一個 package 中,會繼承未做任何存 取控制宣告的成員。 不可繼承成員 如果 Subclass 與 Superclass 在不同 package,所有未宣告有效範圍的 成員全部不繼承。(因為預設式 package access) Superclass 中宣告成 private 的成員

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) Example: Dog. java

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) Example: Dog. java public class Dog { // 屬性 (Variables) private String name; private String color; private int age; // 建構元 (Constructor) public Dog(String name, String color, int age){ this. name = name; this. color = color; this. age = age; } // 方法 (Methods) public void bark() { … } public void handshake() { … } public void rollover(int the. Times) { … }

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) Example: Pomer. java

National Taiwan University Department of Computer Science and Information Engineering 繼承(Inheritance) Example: Pomer. java public class Pomer extends Dog { // 建構元 public Pomer(String name, String color, int age) { super(name, color, age); } // 新的方法 public void proud() { System. out. println("哼. . . "); } }

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 覆蓋

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 覆蓋 Overriding 若繼承下來後,不滿意祖先定義的方法,子孫可以在繼 承以後重新改寫,稱為 Overriding。 可覆蓋成員 • 任何與 Superclass 同名的成員 必覆蓋成員 • Subclass 一定要覆蓋 superclass 中宣告為 abstract 的 methods,除非 subclass 本身也是 abstract 類別 不可覆蓋成員 • Subclass 不可覆蓋 superclass 的 final methods

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 覆蓋

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 覆蓋 Overriding 將 父類別 Dog 的 Handshake 方法 Override 掉 class Pomer extends Dog { // 建構元 public Pomer (String name, String color, String age) { super(name, color, age); } // 將父類別 Dog 中原有的 handshake() 方法 Override 掉 public void handshake() { System. out. println("你的手洗了嗎?"); } public void proud() { System. out. println(“哼. . . "); } }

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 過載

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 過載 Overloading 同一份函式,準備多種定義,以供各種場合呼叫,稱為 Overloading。 建構元也可以利用參數的不同,來達成 overloading

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 建構元的

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 建構元的 Overloading class Pomer extends Dog { // 具有 overloading 的建構元 public Pomer() { super(“Good. Dog”, “Red”, 12); } public Pomer (String name, String color, String age) { super(name, color, age); } // override 父類別 Dog 中的 handshake() 方法 public void handshake() { System. out. println("你的手洗了嗎?"); } public void proud() { System. out. println(“哼. . . "); } }

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 方法的

National Taiwan University Department of Computer Science and Information Engineering 同名異型( Polymorphism ) 方法的 Overloading class Pomer extends Dog { // 建構元 public Pomer (String name, String color, String age) { super(name, color, age); } // 具有 overloading 的 rollover() public void rollover() { for (int i=1; i<=5; i++) System. out. println("我滾 " + i + " 次"); } public void rollover(int thetimes) { for (int i=1; i<=thetimes; i++)…

National Taiwan University Department of Computer Science and Information Engineering super 關鍵字 this &

National Taiwan University Department of Computer Science and Information Engineering super 關鍵字 this & super 關鍵字 this:指的是目前的 class super:指的是父類別 使用 super 來呼叫父類別中的方法 語法:super. method. Name(arg. List); • super. Bark(); • super. Rollover(5); 若子類別中沒有建構元(constructor),可單用 super 來 呼叫父類別中的建構元 語法:super(arg. List);

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 Java

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 Java 中的所有物件,全部繼承自 java. lang. Object 只要程式師沒有以 extends 指定 繼承之物件,Java 會自動用 Object 作為所有物件的父物件。

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 以下是

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 以下是 Object 中的方法,可能是您想 Overriding 的: clone() equals() to. String() finalize() 以下是 Object 中,宣告為 final 的方法,不可以 Overriding: get. Class() hash. Code() notify. All() wait()

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 clone()

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 clone() 原始宣告: protected Object clone() throws Clone. Not. Supported. Exception 作用:複製一份物件本身,並傳回去。 要求: • 要讓類別成為可複製類別的最簡單方法:在類別宣告最後加上 implements Cloneable • Object 提供的 clone() 功能很適合,但有些類別必須要覆蓋 clone(), 才能提供正確的複製功能

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 equals()

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 equals() 原始宣告: public boolean equals(Object obj); 作用:比較兩個物件的內含值是否相等。 要求:無 hash. Code() 回傳的 int 數值,代表物件在 hash table 裡對應位置。 傳回物件在 “雜湊表” (Hash Table) 中的索引值。通常配合 java. util. Hashtable 使用

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 finalize()

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 finalize() 原始宣告: protected void finalize() throws Throwable 作用:物件離開其有效範圍時,一定會被叫用的函式。 用來清除本物件以 new 霸佔的記憶體。 系統會自動呼叫 finalize(),因此大部分的類別都不需要 覆蓋 finalize() 所以在 Java 中,要拋棄一個記憶體,只要: • Obj. A = null; • Obj. B = null;

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 to.

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 to. String() 原始宣告: public String to. String(); 作用:傳回一個此物件的描述字串 to. String() 對除錯(debug)很有幫助 System. out. println(new Double(Math. PI). to. String());

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 改寫從Object物件繼承來的to.

National Taiwan University Department of Computer Science and Information Engineering Java 物件祖先:Object 類別 改寫從Object物件繼承來的to. String()方法 Example: Time. Demo. java class Time { … public String to. String() { return (hour+": "+(minute<10? "0" : "")+minute+ ": "+(second<10? "0" : "")+second); } }

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods Final Classes 定義 • 一個不准被別人繼承的類別稱為 final class 宣告 語法:Access. Level final class Class. Name • 如:final class Color. Point 使用時機 • 安全性 – 駭客最常用來破壞系統的方式,建立某個類別的 subclass,然 後將原來類別的主體置換成自己的主體。 • 當一個類別已經十分完美時

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods Final Classes final class A { //… } // The following class is illegal. class B extends A //錯誤!無法形成A的子類別 { //… }

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods Final Methods 定義 • 一個不准被 subclass 覆寫(Overriding)的函式稱為 final method 宣告 • 語法:final 傳回值型態 函式名稱 (參數, …); 使用時機 • 當覆寫(Overriding)會出現問題時。

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods

National Taiwan University Department of Computer Science and Information Engineering Final Classes and Methods Final Methods class A { final void meth() { System. out. println("This is a final method. "); } } class B extends A { void meth( ) //這裡開始會出現錯誤!不能覆蓋。 { System. out. println("Illegal!"); } }

National Taiwan University Department of Computer Science and Information Engineering 實例講解 程式:Inheritance. Demo. java

National Taiwan University Department of Computer Science and Information Engineering 實例講解 程式:Inheritance. Demo. java 類別成員(Class Method) 繼承(Inheritance) Overriding Overloading super

National Taiwan University Department of Computer Science and Information Engineering Your Turn 試用 Dog

National Taiwan University Department of Computer Science and Information Engineering Your Turn 試用 Dog 類別產生一個 如Golden. Retriever (黃金獵犬)類別 1. 具有兩個建構元(Overloading) • Gold. Retriever() 預設:如流浪狗、金色、5 歲 不傳任何參數時、呼叫overloading的建構元 呼叫方式如 this(“流浪狗”, “金色”, 5); • Gold. Retriever(String the. Name, String the. Color, int the. Age) 初始化其實跟Dog父類別一樣, 呼叫方式如 super(the. Name, the. Color, the. Age); 2. 多一個方法 smile(),如印出 “^_^” 3. Override 原本 Dog 類別中的 bark() 方法,如印出 “嘻嘻~” 4. Override Object 中的 to. String(),如印出“我是一隻可愛的黃金 獵犬!”