欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > PPT文档下载  

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

    • 资源ID:3024363       资源大小:264.51KB        全文页数:51页
    • 资源格式: PPT        下载积分:8
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要8
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    Java 入門與進階設計 第 11 章:繼承與 Overriding,講師:紀俊男 cnchims10.url.com.tw,本章重點,所有物件的祖先 java.lang.Object 抽象類別 abstract class 繼承 可繼承成員,可覆蓋成員,不繼承成員 無後代類別 final class 介面介紹,物件的終極祖先 java.lang.Object,Java 中的所有物件,全部繼承自 java.lang.Object 只要程式師沒有以 extends 指定 superclass,Java 會自動用 Object 作為所有物件的父物件。,java.lang.Object 的方法,以下方法,可能是您想覆蓋的: clone equals toString finalize 以下方法,宣告為 final,不可以覆蓋: getClass hashCode notify notifyAll wait,java.lang.Object 的方法,clone 原始宣告: protected Object clone() throws CloneNotSupportedException 作用:複製一份物件本身,並傳回去。 要求: 蓋寫 clone() 函數的類別,需實作 Cloneable 介面。 蓋寫 clone() 時,要攔截 CloneNotSupportedException,clone 範例 (1),public 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 + “)“); 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),/ Object.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),public 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 int 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 (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; / 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 + “, “ + y + “)“; ,java.lang.Object 的方法,finalize 原始宣告: protected void finalize() throws Throwable 作用:物件離開其有效範圍時,一定會被叫用的函式。用來清除本物件以 new 霸佔的記憶體。 要求: 要丟出 Throwable,供 Java 攔截錯誤。 程式碼最後一行,最好去呼叫 super.finalize(); 因為父類別 (此處為 java.lang.Object) 也有可能需要清除它自己霸佔的記憶體。,finalize 範例 (1),public class FinalizeTest public 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); 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(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 中,有一個一直在工作的背景程式,叫做 “Garbage Collector”。 該程式會來回在記憶體中尋找沒有被任何變數擁有的記憶體。如果有,則將之回收。 所以在 Java 中,要拋棄一個記憶體,只要: Start = null; End = null;,java.lang.Object 的方法,getClass 原始宣告: public final Class getClass(); 作用:可取得一個類別的所有資訊。包括類別名稱,父類別名稱,實作介面名稱,所有成員變數及成員函式,甚至於可以由取回的 class 資料個體化一個物件。 要求:不准覆寫 (Overriding) 傳回值:傳回 java.lang.Class。傳回後,您就可以使用所有 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; 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)” 一章解釋。,java.lang.Object 摘要,所有物件的終極祖先是誰? Object 內部有哪些方法? 試述 clone, equals, toString, finalize 的作用。 試述 getClass, hashCode 的作用。,繼承 (Inheritance),定義 子類別接收父類別的屬性,方法,並加以修改或覆蓋 宣告 class 子類別名稱 extends 父類別名稱 class Line extends GraphicsObject 注意 Java 不支援多重繼承,也就是說,一個子類別只能有一個父類別。,繼承 (Inheritance),可繼承成員 (Inheritable Members) Superclass 中宣告為 public 或 protected 的成員。 如果 Subclass 與 Superclass 在同一個 package 中,會繼承未做任何範圍宣告的成員。 可覆蓋成員 (Overriding Members) 任何與 Superclass 同名的成員 必覆蓋成員 宣告成 abstract 的成員 不可蓋成員 宣告成 final 的成員,繼承 (Inheritance),不繼承成員 如果 Subclass 與 Superclass 在不同 package,所有未宣告有效範圍的成員全部不繼承。 Superclass 中宣告成 private 的成員,繼承範例 (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.println (“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 (“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 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),定義 類別內部有尚未定義實作內容的函式,則此類別必須宣告為 abstract class,該函式需宣告為 abstract 函式。 宣告方法 abstract class ABCD 特性 宣告為 abstract 的類別無法產生實體。例如,您無法對上述類別做: ABCD myABCD = new ABCD();,抽象類別 (abstract class),使用時機 當某類別只是在定義一些抽象概念,並不是想利用此類別實作一個物件時。 範例,GraphicsObject,Rectangle,Circle,Line,abstract,抽象類別實例 (1),public class AbstractTest public static 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; public 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 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 Rectangle(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+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 drawn 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, Point 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),定義 一段只有常數與函式宣告,但沒有函式實作的程式碼。 宣告 public interface 介面名稱 extends 父介面名稱 作用 讓某個功能,不論由誰實作,都能夠有相同的函式名稱,傳入值,傳出值,與存取範圍。,介面 (Interface),例如:,GraphicsObject,Rectangle,Circle,Line,Paintable,介面 (Interface),為何不用 abstract class? 如果把外掛功能做成 abstract class 而不是 interface,會使得需要此外掛功能的 class 一定得繼承這個 abstract class。如此一來,它就不能繼承其它的類別了。 (Java 不支援多重繼承) 因為多重繼承會有問題,但實際上又有多重繼承的需要,所以發明了 interface,作為折衷的替代方案。,介面 (Interface),注意事項 如果某介面繼承另一個介面,則 subclass 能從 superclass 繼承到的只有常數與函式宣告而已。 介面一旦公佈出去,開始供大家使用後,就請盡量不要更改。因為一旦更改,那些已經使用此介面的程式會因無法實作新功能,而不能執行。 如果真要更改,請宣告新介面,其它人就可以決定要實作一個介面或兩個介面都實作。,介面實例 (1),public class InterfaceTest public static void main (String 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.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() 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(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 + “, “ + (Origin.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 代替介面? 介面能由父介面繼承到哪些東西? 介面一旦公佈出去,開始使用後,為什麼不能輕易更改?,

    注意事项

    本文(807-Java 入门与进阶设计第 11 章:继承与 Overriding.ppt)为本站会员(本田雅阁)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




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

    三一文库
    收起
    展开