Thinking in Java 习题 - 第七章

Thinking in Java 习题 - 第七章

Oct 19, 2013
Java

练习2 #

//从Detergent中继承产生一个新的类,覆盖scrub()并添加一个名为sterilize()的新方法。
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();
		System.out.println("Cleanser.main():");
		System.out.println(x);
	}
}
class Detergent extends Cleanser{
	public void scrub (){
		append(" Detegent.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();
		System.out.println("Detergent.main()");
		System.out.println(x);
		Cleanser.main(args);
	}
}
public class Main extends Detergent{
	public void scrub(){
		append(" Main.scrub()");
		super.scrub();
	}
	public  void sterilize(){
		System.out.println("sterilize()");
	}
	public static void main (String args[]){
		Main m = new Main();
		m.apply();
		m.scrub();
		m.dilute();
		m.foam();
		System.out.println("Main.main()");
		System.out.println(m);
		Detergent.main(args);
		m.sterilize();
	}
}/*Output:
Main.main()
Cleanser apply() Main.scrub() Detegent.scrub() scrub() dilute() foam()
Detergent.main()
Cleanser dilute() apply() Detegent.scrub() scrub() foam()
Cleanser.main():
Cleanser dilute() apply() scrub()
sterilize()
*/

练习3、练习4 #

//验证:“即使不为Main()创建构造器,编译器也会合成一个默认的构造器,该构造器将调用积累的构造器。”
class Art{
	Art(){
		System.out.println("Art constructor");
	}
}
class Drawing extends Art{
	Drawing (){
		System.out.println("Drawing constructor");
	}
}
public class Main extends Drawing {
	public static void main (String args[]){
		Main c = new Main();
	}
}/*Output:
Art constructor
Drawing constructor
*/

练习5 #

//创建两个带有默认构造器(空参数)的类A和B,从A中继承产生一个名为C的新类,并在C内创建一个B类的成员,不要给C编写构造器,创建一个C类的对象并观察其结果。
class A{
	A(){
		System.out.println("A()");
	}
}
class B{
	B(){
		System.out.println("B()");
	}
}
class C extends A{
	B b = new B();
}
public class Main{
	public static void main (String args[]){
		new C();
	}
}/*Output:
A()
B()
*/

练习6 #

//如果不在BoardGame()中调用基类构造器,编译器将“抱怨”无法找到Game()形式的构造器。调用基类构造器必须是在导出类构造器中要做的第一件事.
class Game{
	Game (int i){
		System.out.println("Game constuctor");
	}
}
class BoardGame extends Game{
	BoardGame (int i){
		super(i);		//调用基类构造器,去掉这条语句会提示编译错误。
		System.out.println("BoardGame constuctor");
	}
}
public class Chess extends BoardGame{
	Chess(){
		super(11);
		System.out.println("Chess constructor");
	}
	public static void main (String args[]){
		Chess x = new Chess();
	}
}

练习7 #

//修改练习5,使A和B以带参数的构造器取代默认的构造器,为C写一个构造器,并在其中执行所有的初始化。
class A{
	A(int i){
		System.out.println("A()"+i);
	}
}
class B{
	B(int i){
		System.out.println("B()"+i);
	}
}
class C extends A{
	C(int i){
		super(i);
		B b = new B(i);
		System.out.println("C()");
	}
}
public class Main{
	public static void main (String args[]){
		new C(1);
	}
}/*Output:
A()1
B()1
C()
*/

练习8 #

//创建一个基类,它仅有一个非默认构造器,在创建一个导出类,它带有默认构造器和非默认构造器,在导出类的构造器中调用基类的构造器。
class A{
	A(int i){
		System.out.println("A(i)");
	}
}
class B extends A{
	B(int i){
		super(i);
		System.out.println("B(i)");
	}
	
}
public class Main{
	public static void main (String args[]){
		System.out.println("Main()");
		Main m = new Main();
		B b = new B(11);
	}
}/*Output:
Main()
A(i)
B(i)
*/

练习9 #

//创建一个Root类,令其含有名为Component1、Component2、Component3类的各一个实例,从Root中派生一个类Stem,也含有上述各“组成部分”,所有的类都应带有可打印出类的相关信息的默认构造器。
class Component1{
	Component1(){
		System.out.println("Component1()");
	}
}
class Component2{
	Component2(){
		System.out.println("Component2()");
	}
}
class Component3{
	Component3(){
		System.out.println("Component3()");
	}
}
class Root{
	Component1 c1 = new Component1();
	Component2 c2 = new Component2();
	Component3 c3 = new Component3();
	Root(){
		System.out.println("Root()");
	}
}
class Stem extends Root {
	Component1 c1 = new Component1();
	Component2 c2 = new Component2();
	Component3 c3 = new Component3();
	Stem (){
		System.out.println("Stem()");
	}
}
public class Main{
	public static void main (String args[]){
		new Stem();
		System.out.println("Main()");
		Component1 c1 = new Component1();
		Component2 c2 = new Component2();
		Component3 c3 = new Component3();
	}
}/*Output:
Component1()
Component2()
Component3()
Root()
Component1()
Component2()
Component3()
Stem()
Main()
Component1()
Component2()
Component3()
*/

练习11 #

//修改Detergent.java,让它使用代理
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 c = new Cleanser();
		c.apply();
		c.scrub();
		c.dilute();
		System.out.println(c);
	}
}
public class Detergent {
	Cleanser c1 = new Cleanser();
	
	public void dilute(){
		c1.dilute();
	}
	public void apply(){
		c1.apply();
	}
	public void scrub(){
		c1.scrub();
	}
	
	public static void main (String args[]){
		Cleanser.main(args);
	}
}/*Output:
Cleanser apply()scrub()dilute()
*/  发现书里面那个append的用法好纠结,非要用String toString。。。

练习13 #

//创建一个类,他应带有一个 被重载的三次的方法,继承产生一个新类,并添加一个该方法的新的重载定义,展示这四个方法在导出类中是可以被使用的。
package test;
class Home {
	void doh(){
		System.out.println("doh()");
	}
	int doh(int i){
		System.out.println("doh(int)");
		return i;
	}
	char doh(char c){
		System.out.println("doh(char)");
		return c;
	}
}
class Homehome {}
class Milhome extends Home{
	void doh(Homehome h){
		System.out.println("Milhome.doh()");
	}
}
class Main{
	public static void main (String args[]){
		Milhome m=new Milhome();
		m.doh();
		m.doh(1);
		m.doh('c');
		m.doh(new Homehome());
	}
}/*Output:
doh()
doh(int)
doh(char)
Milhome.doh()
*/

练习14 #

//ServiceEngine.java中给Engine添加一个service()方法,并在main()中调用该方法
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 ServiceEngine extends Engine{
	public void service(){
		System.out.println("servise");
	}
}
 class Car{
	public Engine engine = new Engine ();
	public Wheel[] wheel = new Wheel[4];
	 ServiceEngine serviceEngine = new ServiceEngine();
	public Door
		left= new Door(),
		right= new Door();
	public Car(){
		for (int i=0;i<4;i++){
			wheel[i]=new Wheel();
		}
	}
}
 public class ServicebleCar{
	 public static void main (String args[]){
		 Car car = new Car();
		 car.left.window.rollup();
		 car.left.window.rollup();
		 car.serviceEngine.service();
	 }
 }/*Output:
 servise
*/
 

练习15 #

package main.test;

public class Test {
	protected void test(){
		System.out.println("protected test()");
	}
}
package main;
import main.test.*;
public class Main extends Test {
	void Maintest(){
		test();
	}
	public static void main (String args[]){
		Main m = new Main();
		Test t = new Test();
		m.Maintest();
//		t.test(); 编译不能通过。
	}
}/*Output:
protected test()
*/

练习16 #

package main;
//创建一个名为Amphibian的类,由此继承产生一个成为Frog的类,在基类中设置适当的方法,在main()中,创建一个Frog向上转型至Amphibian,
//然后说明所有方法都可工作。
class Amphibian{
	Amphibian(){
		System.out.println("Amphibian");
	}
	void test(){
		System.out.println("Amphibian.haha");
	}
}
class Frog extends Amphibian{
	Frog(){
		super();
		System.out.println("Frog");
	}
	void test(){
		System.out.println("Frog.test");
	}
}
class Main{
	public static void main (String args[]){
		Amphibian a = new Frog();
		a.test();
	}
}/*OutPut:
Amphibian
Frog
Frog.test
*/

练习17 #

package main;
//创建一个名为Amphibian的类,由此继承产生一个成为Frog的类,在基类中设置适当的方法,在main()中,创建一个Frog向上转型至Amphibian,
//然后说明所有方法都可工作。
class Amphibian{
	Amphibian(){
		System.out.println("Amphibian");
	}
	void test(){
		System.out.println("Amphibian.haha");
	}
}
class Frog extends Amphibian{
	Frog(){
		super();
		System.out.println("Frog");
	}
}
class Main{
	public static void main (String args[]){
		Amphibian a = new Frog();
		a.test();
	}
}/*OutPut:
Amphibian
Frog
Amphibian.haha
*/

练习18 #

package main;
import java.util.Random;
//创建一个含有static final域和final域的类,说明二者间的区别。
class Main{
	private static Random rand = new Random();
	private final int I=rand.nextInt(20);
	private static final int II=rand.nextInt(20);
	public static void main (String args[]){
		Main m= new Main();
		System.out.println("A:I="+m.I);
		System.out.println("A:II="+m.II);
		System.out.println();
		Main m1 = new Main();
		System.out.println("B:I="+m1.I);
		System.out.println("B:II="+m1.II);
	}
}/*Output:
A:I=10
A:II=9

B:I=12
B:II=9
*/
本文以 CC 署名-非商业性使用-禁止演绎 4.0 国际 协议进行许可。

上一篇:Thinking in Java 习题 - 第六章

下一篇:数据结构与算法实验课 - 链表