Java編程思想第四版*第七章*個人練習,編程思想第四版
歡迎加群:239063848
進群須知:本群僅用於技術分享與交流,問題發布與解答
禁止閑聊、禁止廣告、禁止招聘……非誠勿擾
練習1:(2)建立一個簡單的類。第二個類中,將一個引用定義為第一個類的對象。運用惰性初始化來執行個體化 這個對象。
package test;public class Manager {public static void main(String args[]){Second s=new Second();s.getFirst();}/** * 列印結果: */}class First{}class Second{First f;Second(){System.out.println("Creating Second");}First lazy(){if(f==null){ System.out.println("Creating First");f=new First();}return f;}public First getFirst(){return lazy();}}
練習2:(2)從Detergent中繼承產生一個新的類。覆蓋scrub()並添加一個名為sterilize()的新方法。
package test;public class Manager {public static void main(String args[]){Sub s=new Sub();s.apply();s.dilute();s.foam();s.scrub();s.sterilize();new print(s);}/** * 列印結果:Cleanser apply() dilute() foam() sub.scrub Detergent.scrub()sub.sterilize() */}class print{print(Object obj){System.out.println(obj);}}class Cleanser{private String s="Cleanser";public void append(String a){s+=a;}public void dilute(){append(" dilute()");}public void apply(){append(" apply()");}public void scrub(){append(" scrub() ");}public String toString(){return s;}public static void main(String[] args){Cleanser x=new Cleanser();x.dilute();x.apply();x.scrub();new print(x);}}class Detergent extends Cleanser{public void scrub(){append(" Detergent.scrub()");}public void foam(){append(" foam()");}}class Sub extends Detergent{public void scrub(){append(" sub.scrub");super.scrub();}public void sterilize(){append("sub.sterilize()");}}練習3(2)證明前面兩句話(即使你不為Cartoon建立構造器,編譯器也為會你合成一個預設的構造器,該構造器將調用基類的構造器)
package test;public class Manager {public static void main(String args[]){new Cartoon();}/** * 列印結果:Art constructor Drawing constructor */}class print{print(Object obj){System.out.println(obj);}}class Art{Art(){new print("Art constructor ");}}class Drawing extends Art{Drawing(){new print("Drawing constructor ");}}class Cartoon extends Drawing{}
練習4(2)證明基類構造器總是會被調用,在匯出類構造器之前被調用。
package test;public class Manager {public static void main(String args[]){new Child();}/** * 列印結果:父類構造器輸出子類構造器輸出 */}class print{print(Object obj){System.out.println(obj);}}class Parent{Parent(){new print("基類構造器輸出");}}class Child extends Parent{Child(){new print("子類構造器輸出");}}
練習5:(1)建立兩個帶有預設構造器(空參數列表)的類A和類B。從A中繼承產生一個名為C的新,並在C內建立一個B類的成員。不要給C編寫構造器。建立一個C類的對象並觀察其結果。
package test;public class Manager {public static void main(String args[]){new C();}/** * 列印結果:A()……B()……B()……C()…… */}class print{print(Object obj){System.out.println(obj);}}class A{A(){new print("A()……");}}class B{B(){new print("B()……");}}class C extends A{private B b=new B();C(){new print("C()……");}private B b2=new B();}
練習6:(1)用Chess證明前面兩名話
package test;public class Manager {public static void main(String args[]){new Chess();}/** * 列印結果:Exception in thread "main" java.lang.Error: Unresolved compilation problem: Implicit super constructor BordGame() is undefined. Must explicitly invoke another constructorat test.Chess.<init>(Manager.java:32)at test.Manager.main(Manager.java:6) */}class print{print(Object obj){System.out.println(obj);}}class Game{Game(int i){new print("Game constructor");}}class BordGame extends Game{BordGame(int i){super(i);new print("BordGame constructor");}}class Chess extends BordGame{Chess(){//Implicit super constructor BordGame() is undefined. Must explicitly invoke another constructornew print("Chess constructor");}}
練習7:(1)修改練習5,使A和B以帶參數的構造器取代預設的構造器。為C寫一個構造器,並在其中執行所有初始化。
package test;public class Manager {public static void main(String args[]){new C();}/** * 列印結果:A()……B()……B()……C()…… */}class print{print(Object obj){System.out.println(obj);}}class A{A(int i){new print("A()……");}}class B{B(){new print("B()……");}}class C extends A{private B b=new B();C(){super(1);//必須new print("C()……");}private B b2=new B();}
練習8:(1)建立一個基類,它僅有一個非預設構造器;再建立一個匯出類,它帶有預設構造器和非預設構造器。在匯出類的構造器中調用基類的構造器。
package test;public class Test { public static void main(String[] args) { } }class A{/** 非預設構造器 **/A(int i){System.out.println("基類");}}class B extends A{B(){super(1);/** 調用基類建構函式 **/}B(int i){super(i);/** 調用基類建構函式 **/System.out.println("匯出類");}}
練習9:(2)建立一個Root類,令其含有名為Component1、Component 2、Component3的類的各一個執行個體(這些也由你寫)。從Root中派生一個類Stem,也含有上述各“組成部分”。所有的類都應帶有可列印出類的相關資訊的預設構造器。
package test;public class Test { public static void main(String[] args) { new Stem(); } /** * 輸出Component1 constructorComponent2 constructorComponent3 constructorRoot constructorComponent1 constructorComponent2 constructorComponent3 constructorStem constructor */}class Root{private Component1 component1=new Component1();private Component2 component2=new Component2();private Component3 component3=new Component3();Root(){System.out.println("Root constructor");}}class Stem extends Root{private Component1 component1=new Component1();private Component2 component2=new Component2();private Component3 component3=new Component3();Stem(){System.out.println("Stem constructor");}}class Component1{Component1(){System.out.println("Component1 constructor");}}class Component2{Component2(){System.out.println("Component2 constructor");}}class Component3{Component3(){System.out.println("Component3 constructor");}}
練習10:(1)修改練習9,使每個類都僅具有非預設的構造器。
package test;public class Test { public static void main(String[] args) { new Stem(1); } /** * 輸出Component1 constructor 1Component2 constructor 2Component3 constructor 3Root constructorComponent1 constructor 1Component2 constructor 2Component3 constructor 3Stem constructor */}class Root{private Component1 component1=new Component1(1);private Component2 component2=new Component2(2);private Component3 component3=new Component3(3);Root(int i){System.out.println("Root constructor");}}class Stem extends Root{private Component1 component1=new Component1(1);private Component2 component2=new Component2(2);private Component3 component3=new Component3(3);Stem(int i){super(i);System.out.println("Stem constructor");}}class Component1{Component1(int i){System.out.println("Component1 constructor "+i);}}class Component2{Component2(int i){System.out.println("Component2 constructor "+i);}}class Component3{Component3(int i){System.out.println("Component3 constructor"+i);}}
練習11:(3)修改Detergent.java。讓它使用代理。
package test;public class Test {public static void main(String args[]){Sub s=new Sub();s.apply();s.dilute();s.foam();s.scrub();s.sterilize();new print(s);}/** * 列印結果:Cleanser apply() dilute() foam() sub.scrub Detergent.scrub()sub.sterilize() */}class print{print(Object obj){System.out.println(obj);}}class Cleanser{private String s="Cleanser";public void append(String a){s+=a;}public void dilute(){append(" dilute()");}public void apply(){append(" apply()");}public void scrub(){append(" scrub() ");}public String toString(){return s;}public static void main(String[] args){Cleanser x=new Cleanser();x.dilute();x.apply();x.scrub();new print(x);}}class Detergent{Cleanser Cleanser=new Cleanser();public void append(String str){Cleanser.append(str);}public void dilute(){append(" dilute()");}public void apply(){append(" apply()");}public String toString(){return Cleanser.toString();}public void scrub(){append(" Detergent.scrub()");}public void foam(){append(" foam()");}}class Sub extends Detergent{public void scrub(){append(" sub.scrub");super.scrub();}public void sterilize(){append("sub.sterilize()");}}
練習12:(3)將一個適當的dispose()方法的階層添加到練習9的所有類中。
package test;public class Test { public static void main(String[] args) { Stem s=new Stem(); try{ s.toString(); }finally{ s.dispose(); } } /** * 輸出Component1 constructorComponent2 constructorComponent3 constructorRoot constructorComponent1 constructorComponent2 constructorComponent3 constructorStem constructorStem disposeRoot dispose */}class Root{private Component1 component1=new Component1();private Component2 component2=new Component2();private Component3 component3=new Component3();Root(){System.out.println("Root constructor");}void dispose(){System.out.println("Root dispose");}}class Stem extends Root{private Component1 component1=new Component1();private Component2 component2=new Component2();private Component3 component3=new Component3();Stem(){System.out.println("Stem constructor");}void dispose(){System.out.println("Stem dispose");super.dispose();}}class Component1{Component1(){System.out.println("Component1 constructor");}void dispose(){System.out.println("Component1 dispose");}}class Component2{Component2(){System.out.println("Component2 constructor");}void dispose(){System.out.println("Component2 dispose");}}class Component3{Component3(){System.out.println("Component3 constructor");}void dispose(){System.out.println("Component3 dispose");}}
練習13:(2)建立一個類,它應帶有一個被重載了三次的方法。繼承產生一個新類,並添加一個該方法的新的重載定義,展示這四個方法在匯出類中都是可以使用的。
package test;public class Test { public static void main(String[] args) { Stem s=new Stem(); s.a(); int i=10; s.a(i); float f=10f; s.a(f); double d=10d; s.a(d); } /** * 輸出a()a(int)10a(float)10.0a(double)10.0 */}class Root{void a(){System.out.println("a()");}void a(int i){System.out.println("a(int)"+ i);}void a(float i){System.out.println("a(float)"+ i);}}class Stem extends Root{void a(double i){System.out.println("a(double)"+ i);}}
練習14:(1)在Car.java中給Engine添加一個service(),並在main()中調用該方法。