807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt

上传人:本田雅阁 文档编号:3024363 上传时间:2019-06-27 格式:PPT 页数:51 大小:264.51KB
返回 下载 相关 举报
807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt_第1页
第1页 / 共51页
807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt_第2页
第2页 / 共51页
807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt_第3页
第3页 / 共51页
807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt_第4页
第4页 / 共51页
807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt_第5页
第5页 / 共51页
点击查看更多>>
资源描述

《807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt》由会员分享,可在线阅读,更多相关《807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt(51页珍藏版)》请在三一文库上搜索。

1、Java 入門與進階設計 第 11 章:繼承與 Overriding,講師:紀俊男 .tw,本章重點,所有物件的祖先 java.lang.Object 抽象類別 abstract class 繼承 可繼承成員,可覆蓋成員,不繼承成員 無後代類別 final class 介面介紹,物件的終極祖先 java.lang.Object,Java 中的所有物件,全部繼承自 java.lang.Object 只要程式師沒有以 extends 指定 superclass,Java 會自動用 Object 作為所有物件的父物件。,java.lang.Object 的方法,以下方法,可能是您想覆蓋的: clon

2、e equals toString finalize 以下方法,宣告為 final,不可以覆蓋: getClass hashCode notify notifyAll wait,java.lang.Object 的方法,clone 原始宣告: protected Object clone() throws CloneNotSupportedException 作用:複製一份物件本身,並傳回去。 要求: 蓋寫 clone() 函數的類別,需實作 Cloneable 介面。 蓋寫 clone() 時,要攔截 CloneNotSupportedException,clone 範例 (1),publi

3、c class CloneTest public static void main(String args) Point FirstPoint = new Point(1,2); Point SecondPoint = (Point)FirstPoint.clone(); System.out.println(“First Point: (“ + FirstPoint.x + “, “ + FirstPoint.y + “)“); System.out.println(“Second Point: (“ + SecondPoint.x + “, “ + SecondPoint.y + “)“)

4、; class Point implements Cloneable / Member Variables int x; int y;,clone 範例 (2),/ Constructors public Point() x = 0; y = 0; public Point(int x, int y) this.x = x; this.y = y; / Member Funcations void draw() System.out.println(“One point has been drawn at (“ + x + “, “ + y + “)“); ,clone 範例 (3),/ Ob

5、ject.clone() (Overriding) protected Object clone () try Point p = (Point)super.clone(); p.x = this.x; p.y = this.y; return p; catch (CloneNotSupportedException e) throw new InternalError(); ,java.lang.Object 的方法,equals 原始宣告: public boolean equals(Object obj); 作用:比較兩個物件的內含值是否相等。 要求:無,equals 範例 (1),pu

6、blic class EqualsTest public static void main(String args) Point FirstPoint = new Point(1,2); Point SecondPoint = new Point(1,2); if (FirstPoint.equals(SecondPoint) System.out.println(“FirstPoint = SecondPoint“); else System.out.println(“FirstPoint != SecondPoint“); class Point / Member Variables in

7、t x; int y; / Constructors public Point() x = 0; y = 0; ,equals 範例 (2),public Point(int x, int y) this.x = x; this.y = y; / Member Funcations void draw() System.out.println(“One point has been drawn at (“ + x + “, “ + y + “)“); / Object.equals() (Overriding) public boolean equals(Point obj) return (

8、this.x = obj.x) ,java.lang.Object 的方法,toString 原始宣告: public String toString(); 作用:傳回一個此物件的描述字串 要求:無,toString 範例 (1),public class toStringTest public static void main(String args) Point FirstPoint = new Point(1,2); System.out.println(FirstPoint.toString(); class Point / Member Variables int x; int y;

9、 / Constructors public Point() x = 0; y = 0; ,toString 範例 (2),public Point(int x, int y) this.x = x; this.y = y; / Member Funcations void draw() System.out.println(“One point has been drawn at (“ + x + “, “ + y + “)“); / Object.toString() (Overriding) public String toString() return “Point (“ + x +

10、“, “ + y + “)“; ,java.lang.Object 的方法,finalize 原始宣告: protected void finalize() throws Throwable 作用:物件離開其有效範圍時,一定會被叫用的函式。用來清除本物件以 new 霸佔的記憶體。 要求: 要丟出 Throwable,供 Java 攔截錯誤。 程式碼最後一行,最好去呼叫 super.finalize(); 因為父類別 (此處為 java.lang.Object) 也有可能需要清除它自己霸佔的記憶體。,finalize 範例 (1),public class FinalizeTest public

11、 static void main(String args) Line MyLine = new Line(1,1,5,5); MyLine.draw(); class Point public int x; public int y; public Point() x = 0; y = 0; ,finalize 範例 (2),public Point(int x, int y) this.x = x; this.y = y; public void draw() System.out.println (“One point has been drawn at “ + x + “, “ + y

12、); class Line / Member Variables public Point Start; public Point End; / Constructor public Line() Start = new Point(0,0); End = new Point(0,0); ,finalize 範例 (3),public Line(int a, int b, int c, int d) Start = new Point(a, b); End = new Point(c, d); public Line(Point p1, Point p2) Start = new Point(

13、p1.x, p1.y); End = new Point(p2.x, p2.y); public void draw() System.out.println (“Line: (“ + Start.x + “, “ + Start.y + “) - (“ + End.x + “, “ + End.y + “)“); protected void finalize() throws Throwable Start = null; End = null; super.finalize(); ,finalize 與 Garbage Collection,在 Java VM 中,有一個一直在工作的背景

14、程式,叫做 “Garbage Collector”。 該程式會來回在記憶體中尋找沒有被任何變數擁有的記憶體。如果有,則將之回收。 所以在 Java 中,要拋棄一個記憶體,只要: Start = null; End = null;,java.lang.Object 的方法,getClass 原始宣告: public final Class getClass(); 作用:可取得一個類別的所有資訊。包括類別名稱,父類別名稱,實作介面名稱,所有成員變數及成員函式,甚至於可以由取回的 class 資料個體化一個物件。 要求:不准覆寫 (Overriding) 傳回值:傳回 java.lang.Class

15、。傳回後,您就可以使用所有 java.lang.Class 的屬性與方法。,getClass 範例 (1),public class getClassTest public static void main(String args) Point FirstPoint = new Point(1,2); System.out.println(“Class name : “ + FirstPoint.getClass().getName(); ,getClass 範例 (2),class Point public int x; public int y; public Point() x = 0;

16、 y = 0; public Point(int x, int y) this.x = x; this.y = y; public void draw() System.out.println (“One point has been drawn at “ + x + “, “ + y); ,java.lang.Object 的方法,hashCode 傳回物件在 “雜湊表” (Hash Table) 中的索引值。通常配合 java.util.Hashtable 使用。 notify, notifyAll, wait 此三個函式用於多工處理。我們將在 “執行緒 (Thread)” 一章解釋。,j

17、ava.lang.Object 摘要,所有物件的終極祖先是誰? Object 內部有哪些方法? 試述 clone, equals, toString, finalize 的作用。 試述 getClass, hashCode 的作用。,繼承 (Inheritance),定義 子類別接收父類別的屬性,方法,並加以修改或覆蓋 宣告 class 子類別名稱 extends 父類別名稱 class Line extends GraphicsObject 注意 Java 不支援多重繼承,也就是說,一個子類別只能有一個父類別。,繼承 (Inheritance),可繼承成員 (Inheritable Memb

18、ers) Superclass 中宣告為 public 或 protected 的成員。 如果 Subclass 與 Superclass 在同一個 package 中,會繼承未做任何範圍宣告的成員。 可覆蓋成員 (Overriding Members) 任何與 Superclass 同名的成員 必覆蓋成員 宣告成 abstract 的成員 不可蓋成員 宣告成 final 的成員,繼承 (Inheritance),不繼承成員 如果 Subclass 與 Superclass 在不同 package,所有未宣告有效範圍的成員全部不繼承。 Superclass 中宣告成 private 的成員,繼

19、承範例 (1),public class InheritTest public static void main (String args) ColorPoint MyPoint = new ColorPoint(1,2,“Yellow“); MyPoint.Draw(); ,繼承範例 (2),class Point public int x; public int y; public Point() x = 0; y = 0; public Point(int x, int y) this.x = x; this.y = y; public void Draw() System.out.pr

20、intln (“One point has been drawn at “ + x + “, “ + y); ,繼承範例 (3),class ColorPoint extends Point public String Color; public ColorPoint() Color = “Black“; public ColorPoint(int x, int y, String newColor) this.x = x; this.y = y; Color = newColor; public void Draw () super.Draw(); System.out.println (“

21、Color: “ + Color); ,無後代類別 (final class),定義 一個不准被別人繼承的類別稱為 final class。 宣告 final class 類別名稱 final class ColorPoint 使用時機 防止他人繼承某類別後,以同名方式存在,行破壞之實。 當一個類別已經十分完美時。,無後代類別範例,如:ColorPoint 類別已經相當完美,您可以將它宣告為,final class ColorPoint extends Point public String Color; public ColorPoint() Color = “Black“; public

22、ColorPoint(int x, int y, String newColor) this.x = x; this.y = y; Color = newColor; public void Draw () super.Draw(); System.out.println (“Color: “ + Color); ,不可覆寫函式 (final method),定義 一個不准被 subclass 覆寫的函式稱為 final method 宣告 final 傳回值型態 函式名稱 (參數, ); 使用時機 當覆寫會出現問題時。,抽象類別 (abstract class),定義 類別內部有尚未定義實作

23、內容的函式,則此類別必須宣告為 abstract class,該函式需宣告為 abstract 函式。 宣告方法 abstract class ABCD 特性 宣告為 abstract 的類別無法產生實體。例如,您無法對上述類別做: ABCD myABCD = new ABCD();,抽象類別 (abstract class),使用時機 當某類別只是在定義一些抽象概念,並不是想利用此類別實作一個物件時。 範例,GraphicsObject,Rectangle,Circle,Line,abstract,抽象類別實例 (1),public class AbstractTest public sta

24、tic void main (String args) Point MyPoint = new Point(1,2); Line MyLine = new Line(1,2,3,4); Rectangle MyRect = new Rectangle (1, 2, 10, 5); Circle MyCircle = new Circle(1,2, 10); MyPoint.Draw(); MyLine.Draw(); MyRect.Draw(); MyCircle.Draw(); ,抽象類別實例 (2),class Point public int x; public int y; publi

25、c Point() x = 0; y = 0; public Point(int x, int y) this.x = x; this.y = y; public void Draw() System.out.println (“One point has been drawn at “ + x + “, “ + y); ,抽象類別實例 (3),abstract class GraphicsObject protected Point Origin; public GraphicsObject() Origin = new Point(0,0); public void moveTo(int

26、newX, int newY) Origin.x = newX; Origin.y = newY; public abstract void Draw(); protected void finalize() throws Throwable Origin = null; super.finalize(); ,抽象類別實例 (4),class Rectangle extends GraphicsObject public int Length; public int Height; public Rectangle() Length = 0; Height = 0; public Rectan

27、gle(int Left, int Top, int Len, int High) Origin.x = Left; Origin.y = Top; Length = Len; Height = High; public void Draw() System.out.println(“Rectangle has been drawn at n“ + “(“ + Origin.x + “, “ + Origin.y + “) - (“ + (Origin.x+Length) + “, “ + Origin.y + “)n“ + “(“ + Origin.x + “, “ + (Origin.y+

28、Height) + “) - (“ + (Origin.x+Length) + “, “ + (Origin.y+Height) + “)“); ,抽象類別實例 (5),class Circle extends GraphicsObject public int Radius; public Circle() Radius = 0; public Circle(int a, int b, int r) Origin.x = a; Origin.y = b; Radius = r; public void Draw() System.out.println(“Circle has been dr

29、awn at n“ + “Center: (“ + Origin.x + “, “ + Origin.y + “)n“ + “Radius: “ + Radius); ,抽象類別實例 (6),class Line extends GraphicsObject public Point End; public Line() End = new Point(0,0); public Line(int a, int b, int c, int d) Origin.x = a; Origin.y = b; End = new Point(c, d); public Line(Point p1, Poi

30、nt p2) Origin.x = p1.x; Origin.y = p1.y; End = new Point(p2.x, p2.y); public void Draw() System.out.println (“Line: (“ + Origin.x + “, “ + Origin.y + “) - (“ + End.x + “, “ + End.y + “)“); protected void finalize() throws Throwable End = null; super.finalize(); ,介面 (Interface),定義 一段只有常數與函式宣告,但沒有函式實作

31、的程式碼。 宣告 public interface 介面名稱 extends 父介面名稱 作用 讓某個功能,不論由誰實作,都能夠有相同的函式名稱,傳入值,傳出值,與存取範圍。,介面 (Interface),例如:,GraphicsObject,Rectangle,Circle,Line,Paintable,介面 (Interface),為何不用 abstract class? 如果把外掛功能做成 abstract class 而不是 interface,會使得需要此外掛功能的 class 一定得繼承這個 abstract class。如此一來,它就不能繼承其它的類別了。 (Java 不支援多重

32、繼承) 因為多重繼承會有問題,但實際上又有多重繼承的需要,所以發明了 interface,作為折衷的替代方案。,介面 (Interface),注意事項 如果某介面繼承另一個介面,則 subclass 能從 superclass 繼承到的只有常數與函式宣告而已。 介面一旦公佈出去,開始供大家使用後,就請盡量不要更改。因為一旦更改,那些已經使用此介面的程式會因無法實作新功能,而不能執行。 如果真要更改,請宣告新介面,其它人就可以決定要實作一個介面或兩個介面都實作。,介面實例 (1),public class InterfaceTest public static void main (String

33、 args) Rectangle rect = new Rectangle(1, 1, 10, 5); rect.Draw(); rect.FillColor(“Blue“); System.out.println(“Area of Rectangle: “ + rect.Area(); ,介面實例 (2),class Point public int x; public int y; public Point() x = 0; y = 0; public Point(int x, int y) this.x = x; this.y = y; public void Draw() System

34、.out.println (“One point has been drawn at “ + x + “, “ + y); ,介面實例 (3),abstract class GraphicsObject protected Point Origin; public GraphicsObject() Origin = new Point(0,0); public void moveTo(int newX, int newY) Origin.x = newX; Origin.y = newY; public abstract void Draw(); protected void finalize

35、() throws Throwable Origin = null; super.finalize(); interface Paintable public void FillColor(String Color); public double Area(); ,介面實例 (4),class Rectangle extends GraphicsObject implements Paintable public int Length; public int Height; public Rectangle() Length = 0; Height = 0; public Rectangle(

36、int Left, int Top, int Len, int High) Origin.x = Left; Origin.y = Top; Length = Len; Height = High; ,介面實例 (4),public void Draw() System.out.println(“Rectangle has been drawn at n“ + “(“ + Origin.x + “, “ + Origin.y + “) - (“ + (Origin.x+Length) + “, “ + Origin.y + “)n“ + “(“ + Origin.x + “, “ + (Ori

37、gin.y+Height) + “) - (“ + (Origin.x+Length) + “, “ + (Origin.y+Height) + “)“); public void FillColor(String Color) System.out.println(“Rectangle has been filled with “ + Color); public double Area() return Length * Height; ,總整理,何謂繼承?宣告一個類別時如何宣告繼承? 何謂多重繼承?Java 可支援多重繼承嗎? 哪些父類別成員可以被繼承?哪些不能被繼承? 哪些父類別成員可以被覆蓋?哪些一定要覆蓋?哪些不能被覆蓋? 何謂 final class 與 final method? 何謂 abstract class? 有什麼特性?如何宣告? abstract class 的使用時機為何? 何謂介面?作用是什麼?如何宣告? 為什麼不用 abstract class 代替介面? 介面能由父介面繼承到哪些東西? 介面一旦公佈出去,開始使用後,為什麼不能輕易更改?,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1