版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Java編程思想,第六章:重復(fù)運用類,重復(fù)運用類,組合 繼承,組合,一個類包含另一個類的對象成員,package com.tju; class X public class Y X x; Y() x = new X(); ,Containment,將 object reference 置于新的 class 內(nèi),/: SprinklerSystem.java ( p192 ) / Composition for code reuse class WaterSource private String s; WaterSource() System.out.println(WaterSource()
2、; s = new String(Constructed); public String toString() return s; /當(dāng)String對象和source相加, / toString自動調(diào)用 public class SprinklerSystem private String valve1, valve2, valve3, valve4; WaterSource source;,int i; float f; void print() System.out.println(valve1 = + valve1); System.out.println(valve2 = + valv
3、e2); System.out.println(valve3 = + valve3); System.out.println(valve4 = + valve4); System.out.println(i = + i); System.out.println(f = + f); System.out.println(source = + source); public static void main(String args) SprinklerSystem x = new SprinklerSystem(); x.print(); /:,Ans: valve1 = null valve2
4、= null valve3 = null valve4 = null i = 0 f = 0.0 source = null,組合: 初始化,當(dāng)定義一個對象變量時使用new方法 構(gòu)造函數(shù)中 使用對象之前初始化: lazy initialization,初始化,/: Bath.java ( p194 ) / Constructor initialization with composition class Soap private String s; Soap() System.out.println(Soap(); s = new String(Constructed); public Str
5、ing toString() return s; ,public class Bath private String / Initializing at point of definition: s1 = new String(Happy), s2 = Happy, s3, s4; Soap castille; int i; float toy; Bath() System.out.println(Inside Bath(); s3 = new String(Joy); i = 47; toy = 3.14f; castille = new Soap(); ,void print() / De
6、layed initialization: if(s4 = null) s4 = new String(Joy); System.out.println(s1 = + s1); System.out.println(s2 = + s2); System.out.println(s3 = + s3); System.out.println(s4 = + s4); System.out.println(i = + i); System.out.println(toy = + toy); System.out.println(castille = + castille); public static
7、 void main(String args) Bath b = new Bath(); b.print(); /:,Ans: Inside Bath() Soap() s1 = Happy s2 = Happy s3 = Joy s4 = Joy i = 47 toy = 3.14 castille = Constructed,繼承,使用 “extends” 關(guān)鍵字,Class A Class B extends A ,繼承: UML(統(tǒng)一建模語言)(Unified Modeling Language),A,B,構(gòu)造函數(shù),父類先于子類被初始化,帶參數(shù)的構(gòu)造函數(shù),使用 “super” 調(diào)用基類
8、的構(gòu)造函數(shù),/: Detergent.java ( p195 ) / Inheritance syntax ,public class Detergent extends Cleanser / Change a method: public void scrub() append( Detergent.scrub(); super.scrub(); / Call base-class version / Add methods to the interface: public void foam() append( foam(); / Test the new class: public st
9、atic void main(String args) Detergent x = new Detergent(); x.dilute(); x.apply(); x.scrub(); x.foam(); x.print(); System.out.println(Testing base class:); Cleanser.main(args); /:,Ans: Cleanser dilute() apply() Detergent.scrub() scrub() foam() Testing base class: Cleanser dilute() apply() scrub(),Bas
10、e class 的初始化:由 base class 向外擴(kuò)散,/: Cartoon.java ( p197 ) / Constructor calls during inheritance class Art Art() System.out.println(Art constructor); class Drawing extends Art Drawing() System.out.println(Drawing constructor); ,public class Cartoon extends Drawing Cartoon() System.out.println(Cartoon
11、constructor); public static void main(String args) Cartoon x = new Cartoon(); /: 該程序的輸出顯示了自動調(diào)用: Art constructor Drawing constructor Cartoon constructor,帶參數(shù)的構(gòu)造函數(shù):使用 super,/: Chess.java ( p198 ) / Inheritance, constructors and arguments class Game Game(int i) System.out.println(Game constructor); clas
12、s BoardGame extends Game BoardGame(int i) super(i); /如果注釋掉,不能通過編譯 System.out.println(BoardGame constructor); ,public class Chess extends BoardGame Chess() super(11); System.out.println(Chess constructor); public static void main(String args) Chess x = new Chess(); /: Ans: Game constructor BoardGame
13、constructor Chess constructor,組合 class Spoon extends Utensil Spoon(int i) super(i); System.out.println(Spoon constructor); class Fork extends Utensil Fork(int i) super(i); System.out.println(Fork constructor); ,class Knife extends Utensil Knife(int i) super(i); System.out.println(Knife constructor);
14、 / A cultural way of doing something: class Custom Custom(int i) System.out.println(Custom constructor); ,public class PlaceSetting extends Custom Spoon sp; Fork frk; Knife kn; DinnerPlate pl; PlaceSetting(int i) super(i + 1); sp = new Spoon(i + 2); frk = new Fork(i + 3); kn = new Knife(i + 4); pl =
15、 new DinnerPlate(i + 5); System.out.println( PlaceSetting constructor); public static void main(String args) PlaceSetting x = new PlaceSetting(9); /:,Ans: Custom constructor Utensil constructor Spoon constructor Utensil constructor Fork constructor Utensil constructor Knife constructor Plate constru
16、ctor DinnerPlate constructor PlaceSetting constructor,清除,沒有象C+中的析構(gòu)函數(shù) 自動喚起垃圾回收器 (不知何時喚起,不知是否喚起) 必須編寫一個特定的方法,編寫一個特定的方法 ( p201 ),public class CADSystem extends Shape . . - private Line l; . for( ) - l = new Line(2, 4); . void cleanup() . . - l.cleanup(); . ,Shape,Circle,Triangle,Line,CADSystem,/: c06:C
17、ADSystem.java ( p201 ) / Ensuring proper cleanup. import java.util.*; class Shape Shape(int i) System.out.println(Shape constructor); void cleanup() System.out.println(Shape cleanup); ,class Circle extends Shape Circle(int i) super(i); System.out.println(Drawing a Circle); void cleanup() System.out.
18、println(Erasing a Circle); super.cleanup(); class Triangle extends Shape Triangle(int i) super(i); System.out.println(Drawing a Triangle); void cleanup() System.out.println(Erasing a Triangle); super.cleanup(); ,class Line extends Shape private int start, end; Line(int start, int end) super(start);
19、this.start = start; this.end = end; System.out.println(Drawing a Line: + start + , + end); void cleanup() System.out.println(Erasing a Line: + start + , + end); super.cleanup(); ,public class CADSystem extends Shape private Circle c; private Triangle t; private Line l; CADSystem(int i) super(i + 1);
20、 l = new Line(2, 4j); c = new Circle(1); t = new Triangle(1); System.out.println(Combined constructor); ,void cleanup() System.out.println(CADSystem.cleanup(); / The order of cleanup is the reverse / of the order of initialization t.cleanup(); c.cleanup(); l.cleanup(); super.cleanup(); public static
21、 void main(String args) CADSystem x = new CADSystem(47); try / Code and exception handling. finally x.cleanup(); /:,Ans: Shape constructor Shape constructor Drawing a line: 2, 4 Shape constructor Drawing a Circle Shape constructor Drawing a Triangle Combind constructor CADSystem.cleanup( ) Erasing a
22、 Triangle Shape cleanup Erasing a Circle Shape cleanup Erasing a line 2,4 Shape cleanup Shape cleanup,重載方法,與C+不同 基類的重載方法是可見的,名稱不被屏蔽 ( not Name Hiding) ( p203 ),/: Hide.java / Overloading a base-class method name / in a derived class does not hide the / base-class versions class Homer char doh(char c
23、) System.out.println(doh(char); return d; float doh(float f) System.out.println(doh(float); return 1.0f; ,class Milhouse class Bart extends Homer void doh(Milhouse m) class Hide public static void main(String args) Bart b = new Bart(); b.doh(1); / doh(float) used b.doh(x); b.doh(1.0f); b.doh(new Mil
24、house(); /: Ans: doh( float ) doh( char ) doh( float ),組合 vs. 繼承,當(dāng)一個對象包含其它對象時選擇組合: 例如,一臺計算機(jī)包含硬盤,顯示器,CPU等 當(dāng)一個對象具有與之相似的其它對象的特點時選擇繼承:例如,Pentium III 繼承并改進(jìn)了80386的指令集 大多數(shù)情況下選擇組合,protected,private成員隨時都是“私有”的,任何人不得訪問 protected關(guān)鍵字的意思是“它本身是私有的,但可由從這個類繼承的任何東西或者同一個包內(nèi)的其他任何東西訪問” Java中的protected會成為進(jìn)入“友好”狀態(tài),向上轉(zhuǎn)型,向上
25、轉(zhuǎn)型(Upcasting):一個繼承類向基類轉(zhuǎn)換(將繼承類 Reference轉(zhuǎn)為基類 Reference); 多態(tài)性 使用繼承時要謹(jǐn)慎,A,B,向上轉(zhuǎn)型,/: c06:Wind.java ( p207 ) / Inheritance / Upcasting: 子類對象傳給父類函數(shù) /: Ans:,Final 關(guān)鍵字,“Final” 意味著最終 “Final” 意味著數(shù)據(jù)是只讀的 可應(yīng)用于字段,方法和類,Final:數(shù)據(jù),數(shù)據(jù)固定不變 可以是永不改變的“編譯期常量” ( compile-time constant) 可以在執(zhí)行期(run-time)被初始化 用于基本數(shù)據(jù)類型:final會將值變
26、成一個常數(shù) 用于對象reference:final會將reference變成一個常數(shù),進(jìn)行聲明時,必須將引用初始化到一個具體的對象。而且永遠(yuǎn)不能將引用變成指向另一個對象。,Final:數(shù)據(jù),/: c06:FinalData.java ( p209 ) / The effect of final on fields.class Value int i = 1; public class FinalData / Can be compile-time constants final int i1 = 9; static final int VAL_TWO = 99; / Typical publi
27、c constant: public static final int VAL_THREE = 39; / Cannot be compile-time constants: final int i4 = (int)(Math.random()*20); static final int i5 = (int)(Math.random()*20);,Value v1 = new Value(); final Value v2 = new Value(); static final Value v3 = new Value(); / Arrays: final int a = 1, 2, 3, 4
28、, 5, 6 ; public void print(String id) System.out.println( id + : + i4 = + i4 + , i5 = + i5); ,public static void main(String args) FinalData fd1 = new FinalData(); /! fd1.i1+; / Error: cant change value fd1.v2.i+; / Object isnt constant! fd1.v1 = new Value(); / OK - not final for(int i = 0; i fd1.a.
29、length; i+) fd1.ai+; / Object isnt constant! /! fd1.v2 = new Value(); / Error: Cant /! fd1.v3 = new Value(); / change reference /! fd1.a = new int3; fd1.print(fd1); System.out.println(Creating new FinalData); FinalData fd2 = new FinalData(); fd1.print(fd1); fd2.print(fd2); /:,Ans: 答案有變化 fd1: i4 = 6,
30、 i5 = 14 Creating new FinalData fd1: i4 = 6, i5 = 14 fd2: i4 = 13, i5 = 14,Final: 參數(shù),一個方法的 Final 參數(shù)是只讀的 無法令該參數(shù)(一個 reference)改指它處,Final: 參數(shù),/: c06:FinalArguments.java ( p211) / Using final with method arguments. class Gizmo public void spin() public class FinalArguments void with(final Gizmo g) /! g
31、= new Gizmo(); / Illegal - g is final void without(Gizmo g) g = new Gizmo(); / OK - g not final g.spin(); ,/ void f(final int i) i+; / Cant change / You can only read from a final primitive: int g(final int i) return i + 1; public static void main(String args) FinalArguments bf = new FinalArguments(
32、); bf.without(null); bf.with(null); /: Ans:,Final: 方法,鎖住這個函數(shù),在繼承類中是不能改變的 效率,轉(zhuǎn)化為 inline 調(diào)用,Final 和 private,一個類中的所有private方法本身就是final,Final: 類,不能從final類中繼承 Final類中的所有方法都是“final”,/: c06:Jurassic.java ( p214 ) / Making an entire class final. class SmallBrain final class Dinosaur int i = 7; int j = 1; Sm
33、allBrain x = new SmallBrain(); void f() /! class Further extends Dinosaur / error: Cannot extend final class Dinosaur public class Jurassic public static void main(String args) Dinosaur n = new Dinosaur(); n.f(); n.i = 40; n.j+; /: Ans:,Final 警告,使用“final”時要謹(jǐn)慎 其它代碼可能希望繼承你的類,Class 裝載,JVM的函數(shù) 當(dāng)需要時載入類,繼承與初始化,對包括繼承在內(nèi)的整個初
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年安徽國防科技職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性考試備考題庫帶答案解析
- 財險承保課件
- 2026年渤海理工職業(yè)學(xué)院單招綜合素質(zhì)考試備考題庫帶答案解析
- 護(hù)理文書規(guī)范書寫與質(zhì)量控制
- 醫(yī)院藥房人員禮儀與患者體驗
- 2026年黑龍江旅游職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性考試備考題庫有答案解析
- 慢性病患者的長期護(hù)理策略
- 2026年河北化工醫(yī)藥職業(yè)技術(shù)學(xué)院單招綜合素質(zhì)筆試備考題庫帶答案解析
- 護(hù)理人員在護(hù)理管理中的職責(zé)
- 醫(yī)院導(dǎo)診服務(wù)禮儀案例分析
- 《性病防治知識講座》課件
- 殘疾人社區(qū)康復(fù)區(qū)建設(shè)方案模版(3篇)
- 山林地租賃合同書范例
- 鋼筋工安全晨會(班前會)
- 2024版《中醫(yī)基礎(chǔ)理論經(jīng)絡(luò)》課件完整版
- 游戲公司運營風(fēng)險控制預(yù)案
- 山東省臨沂市2024-2025學(xué)年高二數(shù)學(xué)上學(xué)期期中試題
- DZ∕T 0248-2014 巖石地球化學(xué)測量技術(shù)規(guī)程(正式版)
- JTJ-T-257-1996塑料排水板質(zhì)量檢驗標(biāo)準(zhǔn)-PDF解密
- 殘疾人法律維權(quán)知識講座
- 瀝青維護(hù)工程投標(biāo)方案技術(shù)標(biāo)
評論
0/150
提交評論