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 constructor

	at test.Chess.(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 constructor
		new 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 constructor
		Component2 constructor
		Component3 constructor
		Root constructor
		Component1 constructor
		Component2 constructor
		Component3 constructor
		Stem 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 1
		Component2 constructor 2
		Component3 constructor 3
		Root constructor
		Component1 constructor 1
		Component2 constructor 2
		Component3 constructor 3
		Stem 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 constructor
		Component2 constructor
		Component3 constructor
		Root constructor
		Component1 constructor
		Component2 constructor
		Component3 constructor
		Stem constructor
		Stem dispose
		Root 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)10
		a(float)10.0
		a(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()中调用该方法。

你可能感兴趣的:(java,Java编程思想*第四版,java)