1. 程式人生 > >Java程式設計思想第七章複用類練習題解答

Java程式設計思想第七章複用類練習題解答

練習題解答

練習1

建立一個簡單的類。第二個類中,將一個引用定義為第一個類的物件。運用惰性初始化來例項化 這個物件。

package ch7;

class Enginee{
    Enginee(){
        System.out.println("Hello world");
    }
}

public class Exercise1{
    Enginee enginee;
    public String toString(){
        if (enginee == null){
            enginee = new Enginee();
        }
        return "Enginee:" + enginee;
    }

    public static void main(String[] args) {
        Exercise1 exercise1 = new Exercise1();
        System.out.println(exercise1.toString());
    }
}

練習2

從Detergent中繼承產生一個新的類。覆蓋scrub()並新增一個名為sterilize()的新方法。

package ch7;
//extends

class Cleaner{
    private String s = "Cleaner";
    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) {
        Cleaner x = new Cleaner();
        x.dilute(); x.apply(); x.scrub();
        System.out.println(x);
    }
}

/**
 *
 */
class Detergent extends Cleaner{
    public void scrub() {
        append(" Detergent.scrub()");
        super.scrub();
    }

    //add method to the interface
    public void foam(){ append(" foam()");}
    //Test new class
    public static void main(String[] args) {
        Detergent x = new Detergent();
        x.dilute();
        x.apply();
        x.scrub();
        x.foam();
        System.out.println(x);
        System.out.println(" Testing base class:");
        Cleaner.main(args);
    }
}

public class Ex2 extends Detergent{
    @Override
    public void scrub() {
        System.out.println("Ex2.scrub()");
        super.scrub();
    }

    public void sterilize(){
        System.out.println(" Ex2.sterilize()");
    }

    public static void main(String[] args) {
        Ex2 ex2 = new Ex2();
        ex2.scrub();
        ex2.sterilize();
        ex2.apply();
        System.out.println(ex2);
    }
    
}
/**output:
 * Ex2.scrub()
 *  Ex2.sterilize()
 * Cleaner Detergent.scrub() scrub() apply()
 *
 * Process finished with exit code 0
 * 
 * **/

練習3

即使你不為Cartoon(這裡為Ex3)建立構造器,編譯器也為會你合成一個預設的構造器,該構造器將呼叫基類的構造器

package ch7;

class Art{
    Art(){
        System.out.println("Art constructor");
    }
}

class Drawing extends Art{
    Drawing(){
        System.out.println("Drawing constructor");
    }
}

public class Ex3 extends Drawing{
    public static void main(String[] args) {
        Ex3 ex3 = new Ex3();
    }
}
/**
output:
Art constructor
Drawing constructor

Process finished with exit code 0
**/

練習4

在練習3的基礎上改一改,修改後如下:

package ch7;

class Art{
    Art(){
        System.out.println("Art constructor");
    }
}

class Drawing extends Art{
    Drawing(){
        System.out.println("Drawing constructor");
    }
}

public class Ex3 extends Drawing{
    Ex3(){
        System.out.println("Ex3 constructor");
    }
    public static void main(String[] args) {
        Ex3 ex3 = new Ex3();
        //test
        new Drawing();
    }
}
/**
 * Art constructor
 * Drawing constructor
 * Ex3 constructor
 * Art constructor
 * Drawing constructor
 *
 * Process finished with exit code 0
 * **/

練習5

package ch7;

/**
class A { A(){ System.out.println("A()");} }

class B extends A { B(){ System.out.println("B()");} }

class C extends A {
    B b = new B(); // will then construct another A and then a B
    public static void main(String[] args) {
        System.out.println("C.A");
        C c = new C(); // will construct an A first
    }
}**/



class A{
    A(){
        System.out.println("fA");
    }
}

class B extends A{
    B(){
        System.out.println("fB");
    }
}

class C extends A{
    void fC(){
        B b = new B();
        System.out.println("fC");
    }
}

public class Ex5 {
    B b = new B();
    public static void main(String[] args) {
        Ex5 ex5 = new Ex5();
    }
}
/**output:
 * fA
 * fB
 *
 * Process finished with exit code 0
 * **/

練習6

class Game {
	Game(int i) {
		 System.out.println("Game constructor");
	}	
}

class BoardGame extends Game {
	BoardGame(int i) {
		//  System.out.println("BoardGame constructor"); // call to super must be first
			// statement in constructor 
		super(i); // else: "cannot find symbol: constructor Game()
		 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();
	}
}
/**output:
 * Game constructor
 * BoardGame constructor
 * Chess constructor
 *
 * Process finished with exit code 0
 * **/

練習7

package ch7;

class A{
    A(int i){
        System.out.println("fA");
    }
}

class B extends A{
    B(int i){
        super(i);
        System.out.println("fB");
    }
}

class C extends A{
    C(){
        super(11);
        System.out.println("fC");
    }
    B b = new B(11);
    public static void main(String[] args) {
        C ex5 = new C();
    }
}

/**
 * fA
 * fA
 * fB
 * fC
 *
 * Process finished with exit code 0
 * **/

練習8

package ch7;

class Base{
    Base(String s, Integer i){
        System.out.println("String: " + s + ", Integer:" + String.valueOf(i));
    }
}

class ExportBase extends Base{
    ExportBase(){
        super("String", 3);
        System.out.println(" ExportBase default");
    }

    ExportBase(String s, Integer i){
        super(s, i);
        System.out.println(" ExportBase String:" + s + ", Integer: " + String.valueOf(i));
    }

    public static void main(String[] args) {
        ExportBase exportBase = new ExportBase();
        ExportBase exportBase2 = new ExportBase("String", 4);
    }
}
/**
 * output:
 * String: String, Integer:3
 *  ExportBase default
 * String: String, Integer:4
 *  ExportBase String:String, Integer: 4
 *
 * Process finished with exit code 0
 * 
 * **/

練習9

package ch7;

class Component1{
    Component1(){
        System.out.println("Component1 constructor");
    }
}
class Component2{
    Component2(){
        System.out.println("Component2 constructor");
    }
}
class Component3{
    Component3(){
        System.out.println("Component3 constructor");
    }
}

class Root {
    Component1 cp1 = new Component1();
    Component2 cp2 = new Component2();
    Component3 cp3 = new Component3();
    Root(){
        System.out.println("Root constructor");
    }
}

class Stem extends Root{
    Component1 cp1 = new Component1();
    Component2 cp2 = new Component2();
    Component3 cp3 = new Component3();
    Stem(){
        System.out.println("Stem constructor");
    }
    public static void main(String[] args) {
        Stem stem = new Stem();
    }
}
/**
 Component1 constructor
 Component2 constructor
 Component3 constructor
 Root constructor
 Component1 constructor
 Component2 constructor
 Component3 constructor
 Stem constructor

 Process finished with exit code 0
 * **/

練習10

package ch7;

class Component1{
    Component1(String s){
        System.out.println("Component1 constructor: " + s);
    }
}
class Component2{
    Component2(String s){
        System.out.println("Component2 constructor: " + s);
    }
}
class Component3{
    Component3(String s){
        System.out.println("Component3 constructor: " + s);
    }
}

class Root {
    Component1 cp1 = new Component1("Component1");
    Component2 cp2 = new Component2("Component2");
    Component3 cp3 = new Component3("Component3");
    Root(){
        System.out.println("Root constructor");
    }
}

class Stem extends Root{
    Component1 cp1 = new Component1("Component1");
    Component2 cp2 = new Component2("Component2");
    Component3 cp3 = new Component3("Component3");
    Stem(){
        System.out.println("Stem constructor");
    }
    public static void main(String[] args) {
        Stem stem = new Stem();
    }
}
/**
 Component1 constructor: Component1
 Component2 constructor: Component2
 Component3 constructor: Component3
 Root constructor
 Component1 constructor: Component1
 Component2 constructor: Component2
 Component3 constructor: Component3
 Stem constructor

 Process finished with exit code 0
 * **/

練習11

package ch7;

class class1{
    private String s;

    public void setS(String s) {
        this.s = s;
    }
    public String getS(String s) {
        return s;
    }

    void up(String s){
        System.out.println("class1-up: " + s);
    }

    void down(String s){
        System.out.println("class1-down: " + s);
    }

    void left(String s){
        System.out.println("class1-left: " + s);
    }

    void right(String s){
        System.out.println("class1-right: " + s);
    }
}

class Detergent1 {
    class1 cla1 = new class1();
    void up(String s){
        System.out.println("Detergent1-up: " + s);
    }

    void down(String s){
        System.out.println("Detergent1-down: " + s);
    }

    void left(String s){
        System.out.println("Detergent1-left: " + s);
    }

    void right(String s){
        System.out.println("Detergent1-right: " + s);
    }
    public static void main(String[] args) {
        String s = "mainString";
        Detergent1 detergent1 = new Detergent1();
        detergent1.down(s);
    }
}
/**output:
 * Detergent1-down: mainString

 Process finished with exit code 0**/

public class Ex11 {
}

練習12

練習13

package ch7;

class cls1{
    public String f(){
        System.out.println("no return");
        return "no return";
    }

    public String f(String s){
        System.out.println( "no return, String:" + s);
        return "no return, String:" + s;
    }

    public String f(char s){
        System.out.println( "no return, char: " + s);
        return "no return, char: " + s;
    }
}

class overloading extends cls1{
    public String f(Integer integer){
        System.out.println( "no return, Integer:" + String.valueOf(integer));
        return "no return, Integer:" + String.valueOf(integer);
    }

    public static void main(String[] args) {
        overloading ol = new overloading();
        ol.f(2);
        ol.f("hello");
        ol.f('c');
        ol.f();
    }
}
/*
* output:
* no return, Integer:2
no return, String:hello
no return, char: c
no return

Process finished with exit code 0
* */

練習14

package ch7;

class Engine{
    public void start(){}
    public void rev(){}
    public void stop(){}
}

class Wheel{
    public void inflate(int psi){}
}

class Window{
    public void rollup() {}
    public void rolldown() {}
}

class Door{
    public Window window = new Window();
    public void open(){}
    public void close(){}
}

class Servie{
    {System.out.println("Service");}
}

class Car {
    public Engine engine = new Engine();
    public Wheel[] wheel = new Wheel[4];
    public Door
        left = new Door(),
        right = new Door(); //2-door
    public  Car(){
        for (int i = 0; i < 4; i ++){
            wheel[i] = new Wheel();
        }
    }

    public static void main(String[] args) {
        Car car = new Car();
        car.left.window.rollup();
        car.wheel[0].inflate(72);
        Servie servie = new Servie();
    }

}
/**
 * output:
 * Service
 *
 * Process finished with exit code 0
 * **/

public class P137 {
}

練習15

package ch7;

class Apple{
    private String s = "";
    public Apple(){
        System.out.println("Apple constructor");
    }
    protected String f(String s){
        this.s = s;
        return s;
    }
}

class Fruit{
    public static void main(String[] args) {
        Apple apple = new Apple();
        System.out.println(apple.f("hello world"));
    }
}

/**
 * Apple constructor
 * hello world
 *
 * Process finished with exit code 0
 * **/

練習16

package ch7;

class Amphibian {
    protected void swim() {
        System.out.println("Amphibian swim");
    }
    protected void speak() {
        System.out.println("Amphibian speak");
    }
    void eat() {
        System.out.println("Amphibian eat");
    }
    static void grow(Amphibian a) {
        System.out.println("Amphibian grow");
        a.eat();
    }
}

class Frog extends Amphibian {
    public static void main(String[] args) {
        Frog f = new Frog();
        // call base-class methods:
        f.swim();
        f.speak();
        f.eat();
        // upcast Frog to Amphibian argument:
        Amphibian.grow(f);
    }
}
/**
 * output:
 * Amphibian swim
 * Amphibian speak
 * Amphibian eat
 * Amphibian grow
 * Amphibian eat
 *
 * Process finished with exit code 0
 * **/

public class Ex16 {
}

練習17

class Amphibian {
    protected void swim() {
        System.out.println("Amphibian swim");
    }
    protected void speak() {
        System.out.println("Amphibian speak");
    }
    void eat() {
        System.out.println("Amphibian eat");
    }
    static void grow(Amphibian a) {
        System.out.println("Amphibian grow");
        a.eat();
    }
}

class Frog17 extends Amphibian {
    @Override protected void swim() {
        System.out.println("Frog swim");
    }
    @Override protected void speak() {
        System.out.println("Frog speak");
    }
    @Override void eat() {
        System.out.println("Frog eat");
    }
    static void grow(Amphibian a) {
        System.out.println("Frog grow");
        a.eat();
    }
    public static void main(String[] args) {
        Frog17 f = new Frog17();
        // call overridden base-class methods:
        f.swim();
        f.speak();
        f.eat();
        // upcast Frog17 to Amphibian argument:
        f.grow(f);
        /*Frog grow
        Frog eat
        */
        // upcast Frog17 to Amphibian and call Amphibian method:
        Amphibian.grow(f);
        //Amphibian grow
        //Frog eat
    }
}

/**
 * Frog swim
 * Frog speak
 * Frog eat
 * Frog grow
 * Frog eat
 * Amphibian grow
 * Frog eat
 *
 * Process finished with exit code 0
 * **/

過載和重寫的區別

方法的重寫(Overriding)和過載(Overloading)是java多型性的不同表現,重寫是父類與子類之間多型性的一種表現,過載可以理解成多型的具體表現形式。

(1)方法過載是一個類中定義了多個方法名相同,而他們的引數的數量不同或數量相同而型別和次序不同,則稱為方法的過載(Overloading)。
(2)方法重寫是在子類存在方法與父類的方法的名字相同,而且引數的個數與型別一樣,返回值也一樣的方法,就稱為重寫(Overriding)。
(3)方法過載是一個類的多型性表現,而方法重寫是子類與父類的一種多型性表現。

在這裡插入圖片描述

final和final static 的區別

Ref:
1、組合、繼承、代理的聯絡與區別
2、重寫和過載的區別
3、final和final static 的區別