Managing Inheritance Outline this Access Control static static

  • Slides: 58
Download presentation
Managing Inheritance

Managing Inheritance

Outline this 存取控制(Access Control) static 關鍵字 實體成員與類別成員 static 初值設定區塊 Your Turn 物件導向語言三大特性 封裝 (Encapsulation)

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

this 關鍵字 可以透過 this,呼叫相同類別底下的另一個建構元 public class AClass { private int x, y; private int

this 關鍵字 可以透過 this,呼叫相同類別底下的另一個建構元 public class AClass { private int x, y; private int width, height; public AClass(){ this(0, 0, 0, 0); } public AClass(int width, int height){ this(0, 0, width, height); } public AClass(int x, int y, int width, int height){ this. x = x; this. y = y; this. width = width; this. height = height; }. . . }

this 關鍵字 具有二個建構元的 Box 類別 class Box { double width, height, depth; // 預設的建構元

this 關鍵字 具有二個建構元的 Box 類別 class Box { double width, height, depth; // 預設的建構元 Box() { this(1, 2, 3); } // 可以自行指定長寬的建構元 Box(int width, int height, int depth){ this. width = width; this. height = height; this. depth = depth; } }

this 關鍵字 Example: Box. Demo. java class Box. Demo { public static void main

this 關鍵字 Example: Box. Demo. java class Box. Demo { public static void main (String args []) { //宣告、配置與初始化Box物件 Box my. Box 1 = new Box(); Box my. Box 2 = new Box(10, 20, 30); double vol 1, vol 2; //顯示第一個盒子的體積 my. Box 1. show. Volume(); //獲得第二個盒子的體積 my. Box 2. show. Volume(); } }

Your Turn Hint class Rectangle { final int DEFAULT_LENGTH = 8; final int DEFAULT_WIDTH

Your Turn Hint class Rectangle { final int DEFAULT_LENGTH = 8; final int DEFAULT_WIDTH = 4; private int length; private int width; Rectangle(){ … } Rectangle(int length, int width){ … } Rectangle(Rectangle obj){ … } void show. LW(){ … } double get. Area(){ … } void draw. Rect(){ … } public static void main(String[] args){ … } }

存取控制(Access Control) Example: Alpha. java private int iamprivate = 1; int iampackage = 2;

存取控制(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"); }

存取控制(Access Control) Example: Delta. One. java package One; public class Delta. One { public

存取控制(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 } }

存取控制(Access Control) Example: Delta. Two. java package Two; import One. *; public class Delta.

存取控制(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 } }

static 關鍵字 實體成員與類別成員 變數或 methods 若宣告為 static,則此變數或 methods 即 為類別變數(class variables)或類別方法(class methods) 宣告為 static

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)

static 關鍵字 - 實體成員與類別成員 Example: Static. Demo. java public class Static. Demo { public

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) { … } }

static 初值設定區塊 class 變數或是實體變數,可以直接設定初值 public class Bed. And. Breakfast { public static final int

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,也無法做錯誤處理

static 初值設定區塊 Java 允許將 static 變數集合起來進行初始化動作 語法: class Test. Class{ static int x; static

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。

Your Turn Hint // 建構元 private Rectangle(){ … } // 另一個建構元 private Rectangle(int length,

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(){ … }

封裝 (Encapsulation) 將資料(屬性)與方法(行為)封裝在一個物件裡頭,物件 裡頭的資料與方法被緊緊的綁在一起,並擁有資訊隱藏( Information hiding)的特性。 Example: Time. Demo. java class Time { private

封裝 (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() { … } }

繼承(Inheritance) Example: Dog. java public class Dog { // 屬性 (Variables) private String name;

繼承(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) { … }

繼承(Inheritance) Example: Pomer. java public class Pomer extends Dog { // 建構元 public Pomer(String

繼承(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("哼. . . "); } }

同名異型( Polymorphism ) 覆蓋 Overriding 將 父類別 Dog 的 Handshake 方法 Override 掉 class

同名異型( 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(“哼. . . "); } }

同名異型( Polymorphism ) 建構元的 Overloading class Pomer extends Dog { // 具有 overloading 的建構元

同名異型( 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(“哼. . . "); } }

同名異型( Polymorphism ) 方法的 Overloading class Pomer extends Dog { // 建構元 public Pomer

同名異型( 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++)…

Java 物件祖先:Object 類別 以下是 Object 中的方法,可能是您想 Overriding 的: clone() equals() to. String() finalize() 以下是

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

Java 物件祖先:Object 類別 to. String() 原始宣告: public String to. String(); 作用:傳回一個此物件的描述字串 to. String() 對除錯(debug)很有幫助

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

Java 物件祖先:Object 類別 Example: Time. Demo. java class Time { … public String to.

Java 物件祖先:Object 類別 Example: Time. Demo. java class Time { … public String to. String() { return (hour+": "+(minute<10? "0" : "")+minute+ ": "+(second<10? "0" : "")+second); } }

Final Classes and Methods Final Classes final class A { //… } // The

Final Classes and Methods Final Classes final class A { //… } // The following class is illegal. class B extends A //錯誤!無法形成A的子類別 { //… }

Final Classes and Methods Final Methods class A { final void meth() { System.

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!"); } }

實例講解 程式:Inheritance. Demo. java 類別成員(Class Method) 繼承(Inheritance) Overriding Overloading super

實例講解 程式:Inheritance. Demo. java 類別成員(Class Method) 繼承(Inheritance) Overriding Overloading super

Inheritance. Demo. java class AA { protected int x; //private int x; AA(String s)

Inheritance. Demo. java class AA { protected int x; //private int x; AA(String s) { System. out. println("AA is created. "); System. out. println("your msg: " + s); } public void sample. A() { System. out. println("This is sample AA. "); } // Class Method static void print. Msg(AA obj) { System. out. println(obj. x); } }

Inheritance. Demo. java class BB extends AA { BB() { super("Hi. Hi~~"); // 使用父類別

Inheritance. Demo. java class BB extends AA { BB() { super("Hi. Hi~~"); // 使用父類別 AA 的建構元 System. out. println("BB is created. "); } //overriding public void sample. A() { super. sample. A(); // 使用父類別 AA 的 sample. A() 方法 System. out. println("This is sample BB. "); } // overloading public void sample. A(String s) { System. out. println(s); } // Class Method static void print. Msg(BB obj) { System. out. println(obj. x); }

Inheritance. Demo. java public class Inheritance. Demo { public static void main(String[] args) {

Inheritance. Demo. java public class Inheritance. Demo { public static void main(String[] args) { AA a. Obj = new AA("Hi. . "); a. Obj. x = 100; System. out. println(a. Obj. x); a. Obj. sample. A(); AA. print. Msg(a. Obj); System. out. print("========n"); BB b. Obj = new BB(); b. Obj. x = 200; System. out. println(b. Obj. x); b. Obj. sample. A("Hello!!"); AA. print. Msg(b. Obj); BB. print. Msg(b. Obj); } }