Thinking in Java - Fourth Edition 章節練習個人解答——第7章

 注:本練習題答案繫個人閱讀後所作解答,非原書所配答案。(由於水平有限,文中出現錯誤還望諒解與指正)

 

文中所有源代碼皆在Eclipse-jee-indigo-3.7,Java SE 6平臺下測試運行。


練習1:

package lib.seventh;

class Bath {
	public Bath() {
		System.out.print("Bath()");
	}
}
public class Exercise_1 {
	private Bath bath;
	public String toString() {
	if(bath == null)
		bath = new Bath();
	return bath.toString();
	}
	public static void main(String[] args) {
		Exercise_1 e = new Exercise_1();
		System.out.print(e);
	}
} /* Output:
Bath()lib.seventh.Bath@a90653
*///:~


練習2:

package lib.seventh;

import static net.util.Print.*;

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();
		print(x);
	}
}

class Detergent extends Cleanser {
	public void scrub() {
		append(" Detergent.scrub()");
		super.scrub();
	}
	public void foam() { append(" foam()"); }
	public static void main(String[] args) {
		Detergent x = new Detergent();
		x.dilute();
		x.apply();
		x.scrub();
		x.foam();
		print(x);
		print("Testing base class:");
		Cleanser.main(args);
	}
}

public class Exercise_2 extends Detergent {
	public void surub() {
		append(" Exercise_2.surub()");
		super.scrub();
	}
	public void aterilize() { append(" aterilize()"); }
	public static void main(String[] args) {
		Exercise_2 x = new Exercise_2();
		x.dilute();
		x.apply();
		x.foam();
		x.scrub();
		x.aterilize();
		print(x);
		print("Testing base class:");
		Detergent.main(args);
	}
} /* Output:
Cleanser dilute() apply() foam() Detergent.scrub() scrub() aterilize()
Testing base class:
Cleanser dilute() apply() Detergent.scrub() scrub() foam()
Testing base class:
Cleanser dilute() apply() scrub()
*///:~


練習3:

package lib.seventh;

import static net.util.Print.*;

class Art {
	Art() { print("Art constructor"); }
}

class Drawing extends Art {
	Drawing() { print("Drawing constructor"); }
}
	
public class Cartoon extends Drawing {
	public static void main(String[] args) {
		new Cartoon();
	}
} /* Output:
Art constructor
Drawing constructor
*///:~


練習4:

package lib.seventh;

import static net.util.Print.*;

class Discipline {
	public int credit;
	Discipline() { 
		credit = 0; 
		print("Discipline credit is: " + credit);
	}
}

public class Exercise_4 extends Discipline {
	Exercise_4(int c) {
		this.credit = c;
		print("Exercise_4 credit is: " + credit);
	}
	public static void main(String[] args) {
		new Exercise_4(3);
	}
} /* Output:
Discipline credit is: 0
Exercise_4 credit is: 3
*///:~


練習5:

package lib.seventh;

import static net.util.Print.*;

class A {
	A() { print("A constructor"); }
}
	
class B {
	B() { print("B constructor"); } 
}

public class C extends A {
	B b = new B();
	public static void main(String[] args) {
		new C();
	}
} /* Output:
A constructor
B constructor
*///:~


練習6:

package lib.seventh;

import static net.util.Print.*;

class BoardGame {
	BoardGame(int i) { print("BoardGame constructor"); }
}

public class Chess extends BoardGame {
	Chess() { print("Chess constructor"); }
	public static void main(String[] args) {
		new Chess();
	}
} ///:~


練習7:

package lib.seventh;

import static net.util.Print.*;

class A1 {
	A1(int i) { print("A1 constructor: " + i); }
}
	
class B1 {
	B1(int i) { print("B1 constructor: " + i); } 
}

public class C1 extends A1 {
	B1 b;
	C1(int i) {
		super(i);
		b = new B1(i);
	}
	public static void main(String[] args) {
		new C1(1);
	}
} /* Output:
A1 constructor: 1
B1 constructor: 1
*///:~


練習8:

package lib.seventh;

class Base_8 {
	Base_8(int i) {}
}

public class Exercise_8 extends Base_8 {
	Exercise_8() { super(0); }
	Exercise_8(int i) { super(i); }
} ///:~


練習9:

package lib.seventh;

import static net.util.Print.*;

class Component1 {
	Component1() { print("Component1 constructor"); }
}

class Component2 {
	Component2() { print("Component2 constructor"); }
}

class Component3 {
	Component3() { print("Component3 constructor"); }
}

public class Root {
	Component1 c1;
	Component2 c2;
	Component3 c3;
	Root() {
		c1 = new Component1();
		c2 = new Component2();
		c3 = new Component3();
		print("Root constructor");
	}
}

class Stem extends Root {
	Stem() { print("Stem constructor"); }
	public static void main(String[] args) {
		new Stem();
	}
} /* Output:
Component1 constructor
Component2 constructor
Component3 constructor
Root constructor
Stem constructor
*///:~


練習10:

package lib.seventh;

import static net.util.Print.*;

class Component11 {
	Component11(int i) { print("Component11 constructor"); }
}

class Component12 {
	Component12(int i) { print("Component12 constructor"); }
}

class Component13 {
	Component13(int i) { print("Component13 constructor"); }
}

public class Root1 {
	Component11 c1;
	Component12 c2;
	Component13 c3;
	Root1(int i) {
		c1 = new Component11(i);
		c2 = new Component12(i);
		c3 = new Component13(i);
		print("Root1 constructor");
	}
}

class Stem1 extends Root1 {
	Stem1(int i) { 
		super(i);
		print("Stem1 constructor"); 
	}
	public static void main(String[] args) {
		new Stem1(1);
	}
} /* Output:
Component11 constructor
Component12 constructor
Component13 constructor
Root1 constructor
Stem1 constructor
*///:~


練習11:

package lib.seventh;

import static net.util.Print.*;

public class Detergents {
	public Cleanser c = new Cleanser();
	public void foam() { c.append(" foam()"); }
	public static void main(String[] args) {
		Detergents x = new Detergents();
		x.c.dilute();
		x.c.apply();
		x.c.scrub();
		x.foam();
		print(x.c);
		print("Testing base class:");
		Cleanser.main(args);
	}
} /* Output:
Cleanser dilute() apply() scrub() foam()
Testing base class:
Cleanser dilute() apply() scrub()
*///:~


練習12:

package lib.seventh;

import static net.util.Print.*;

class Component21 {
	Component21() { print("Component21 constructor"); }
	void dispose() { print("Erasing Component21"); }
}

class Component22 {
	Component22() { print("Component22 constructor"); }
	void dispose() { print("Erasing Component22"); }
}

class Component23 {
	Component23() { print("Component23 constructor"); }
	void dispose() { print("Erasing Component23"); }
}

public class Root12 {
	Component21 c1;
	Component22 c2;
	Component23 c3;
	Root12() {
		c1 = new Component21();
		c2 = new Component22();
		c3 = new Component23();
		print("Root12 constructor");
	}
	void dispose() {
		print("Erasing Root12");
		c1.dispose();
		c2.dispose();
		c3.dispose();		 
	}
}

class Stem12 extends Root12 {
	Stem12() { print("Stem12 constructor"); }
	void dispose() { 
		print("Erasing Stem12");
		super.dispose();
	}
	public static void main(String[] args) {
		Stem12 x = new Stem12();
		try{
		} finally {
			x.dispose();
		}
	}
} /* Output:
Component21 constructor
Component22 constructor
Component23 constructor
Root12 constructor
Stem12 constructor
Erasing Stem12
Erasing Root12
Erasing Component21
Erasing Component22
Erasing Component23
*///:~


練習13:

package lib.seventh;

import static net.util.Print.*;

class Aa {
	void paint() {}
}

class Bb extends Aa {
	char paint(char c) {
		print("paint(char)");
		return c;
	}
}

class Cc extends Bb {
	int paint(int i) {
		print("paint(int)");
		return i;
	}
}

class Dd extends Cc {
	float paint(float f) {
		print("paint(float");
		return f;
	}
}

public class Exercise_13 extends Dd {
	Aa paint(Aa a) {
		print("paint(Aa)");
		return a;
	}
	public static void main(String[] args) {
		Exercise_13 e = new Exercise_13();
		e.paint('x');
		e.paint(1);
		e.paint(1.0f);
		e.paint(e);
	}
} /* Output:
paint(char)
paint(int)
paint(float
paint(Aa)
*///:~


練習14:

package lib.seventh;

class Engine {
	public void start() {}
	public void rev() {}
	public void stop() {}
	public void service() {}
}
public class Car {
	public Engine engine = new Engine();
	public static void main(String[] args) {
		Car car = new Car();
		car.engine.service();
	}
} ///:~


練習15:

package lib.seventh.protetcted;

public class Exercise_15 {
	private String name;
	protected void set(String n) { name = n; }
	public Exercise_15(String n) { name = n; }
	public String toString() {
		return "Exercise_15 " + name + ".";
	}
} ///:~
package lib.seventh;

class callSet {
	lib.seventh.protetcted.Exercise_15 e = 
			new lib.seventh.protetcted.Exercise_15("set");
	// e.set("Call set()");
}
public class Exercise_15 extends lib.seventh.protetcted.Exercise_15 {
	Exercise_15(String n) { super(n); }
	public static void main(String[] args) {
		Exercise_15 e = new Exercise_15("set");
		e.set("Call set()");
		System.out.print(e);
	}
} /* Output:
Exercise_15 Call set().
*///:~


練習16:

package lib.seventh;

public class Amphibian {
	private String name;
	public void set(String nm) { name = nm; }
	public String get(Amphibian a) { return name; }
	public String toString() {
		return "Amphibian: " + name;
	}
}

class Frog extends Amphibian {
	public String toString() {
		return super.toString() + " Frog";
	}
	public static void main(String[] args) {
		Frog x = new Frog();
		x.set("frog");
		x.get(x);
		System.out.print(x);
	}
} /* Output:
Amphibian: frog Frog
*///:~


練習17:

package lib.seventh;

public class Amphibian2 {
	private String name;
	public void set(String nm) { name = nm; }
	public String get(Amphibian2 a) { return name; }
	public String toString() {
		return "Amphibian: " + name;
	}
}

class Frog2 extends Amphibian2 {
	public void set(String nm) { super.set(nm + " Frog2"); }
	public String get(Amphibian2 a) { return super.get(a) + "!"; }
	public String toString() {
		return super.toString() + " Frog";
	}
	public static void main(String[] args) {
		Frog2 x = new Frog2();
		x.set("frog2");		
		System.out.print(x.get(x));
	}
} /* Output:
frog2 Frog2!
*///:~


練習18:

package lib.seventh;

public class Exercise_18 {
	static final double PI = 3.1415;
	final double i;
	Exercise_18() {
		i = Math.random() * 10;
	}
	public static void main(String[] args) {
		for(int i = 0; i < 3; i++)
			System.out.println(PI + ", " + new Exercise_18().i);
	}
} /* Output: (50% match)
3.1415, 9.04591089174885
3.1415, 4.842709330596354
3.1415, 5.386041794758788
*///:~


 練習19:

package lib.seventh;

class Pop {
	private int i;
	Pop(int i) { this.i = i; }
}
public class Exercise_19 {
	private final Pop p;
	Exercise_19() {
		p = new Pop(1);
	}
	Exercise_19(int i) {
		p = new Pop(i);
	}
	public static void main(String[] args) {
		new Exercise_19();
		Exercise_19 e = new Exercise_19(13);
		// e.p = new Pop(0); // Can't change
	}
} ///:~


練習20: 

package lib.seventh;

class Homer {
	private final char doh(char c) { return c; }
}
public class Exercise_20 extends Homer {
	@Override
	private final void doh(float f) {}
	public static void main(String[] args) {
		new Exercise_20();
	}
} ///:~


練習21:

package lib.seventh;

class Vector {
	final int ver() { return 1; }
}

public class Exercise_21 extends Vector {
	// final int ver() { return 13; } // Can't cover
	public static void main(String[] args) {
	}
} ///:~



練習22:

package lib.seventh;

final class Vectors {}

// public class Exercise_22 extends Vectors {} // Can't extends
///:~


練習23:

package lib.seventh;

class Insect {
	private static int i = 0;
	static { i++; }
	Insect() { System.out.println("Insect i is: " + i); }
}

public class Exercise_23 {
	private static int i = 0;
	static { i++; }
	public static void print() {
		System.out.println("Exercise_23 i is: " + i); 
	}
	public static void main(String[] args) {
		print();
		new Insect();
	}
} /* Output:
Exercise_23 i is: 1
Insect i is: 1
*///:~


練習24:

package lib.seventh;

import java.awt.Color;

public class Beetle {
	private String name;
	private float size;
	private Color color;
	private boolean pest;
	Beetle(final String name, float size, Color color, boolean pest) {
		this.name = name;
		this.size = size;
		this.color = color;
		this.pest = pest;
	}
	public String toString() {
		return name + " Beetle: size " + size + ", color " +
	color + ", pest " + pest;
	}
}

class Longicorn extends Beetle {
	Longicorn() {
		super("Longicorn", 6, Color.BLACK, true);
	}
	public static void main(String[] args) {
		Longicorn l = new Longicorn();
		System.out.print(l.toString());
	}
} /* Output:
Longicorn Beetle: size 6.0, color java.awt.Color[r=0,g=0,b=0], pest true
*///:~


 

-END-

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章