Thinking in Java - Fourth Edition 章节练习个人解答——第6章

  注:本练习题答案系个人阅读后所作解答,非原书所配答案。(由于水平有限,文中出现错误还望谅解与指正)

 

文中所有源代码皆在Eclipse-jee-indigo-3.7,Java SE 6平台下测试运行。


 

练习1:

package net.util;

public class Print {
	public static void print() {
		System.out.println();
	}
	public static void print(Object o) {
		System.out.println(o);
	}
	public static void printnb(Object o) {
		System.out.print(o);
	}
} ///:~
package lib.sixth;

import net.util.*;

public class Exercise_1 {
	public static void main(String[] args) {
		Print.print("Hello!");
	}
} /* Output:
Hello!
*///:~


练习2:

package net.util;

public class Vector {
	Object[] o;
	public Vector() {
		o = new Object[10];
	}
} ///:~
package lib.sixth;

import net.util.*;
import java.util.*;

public class Exercise_2 {
	public static void main(String[] args) {
		Vector v = new Vector();
	}
} ///:~


练习3:

package lib.sixth.debug;

public class Debug {
	public static void debug(String s) {
		System.out.print(s);
	}
} ///:~
package lib.sixth.debugoff;

public class Debug {
	public static void debug(String s) {		
	}
} ///:~
package lib.sixth;

import lib.sixth.debug.*; // (1)
// import lib.sixth.debugoff.*; // (2)

public class Exercise_3 {
	public static void main(String[] args) {
		Debug.debug("Error!");
	}
} /* Output:
Error!
*///:~


练习4:

package lib.sixth.exer4;

public class Exercise_4_1 {
	protected static void bite() {
		System.out.print("Hello!");
	}
} ///:~
package lib.sixth.exer4;

public class Exercise_4_2 {
	public static void main(String[] args) {
		Exercise_4_1.bite();
	}
} /* Output:
Hello!
*///:~
package lib.sixth;

import lib.sixth.exer4.*;

public class Exercise_4 {
	public static void main(String[] args) {
		Exercise_4_1.bite();
	}
} ///:~


练习5:

package lib.sixth;

public class Exercise_5 {
	public String s1 = "s1";
	protected String s2 = "s2";
	String s3 = "s3";
	private String s4 = "s4";	
	public String getS1() {
		return s1;
	}
	protected String getS2() {
		return s2;
	}
	String getS3() {
		return s3;
	}
	private String getS4() {
		return s4;
	}
} ///:~
package lib.sixth;

public class Exercise_5_1 {
	public static void main(String[] args) {
		Exercise_5 e = new Exercise_5();
		String[] s = new String[]{e.s1, e.s2, e.s3, e.s4};
		e.getS1();
		e.getS2();
		e.getS3();
		e.getS4();
	}
} ///:~


练习6:

package lib.sixth;

public class Exercise_6 {
	protected String s = "Hello!";
	void getS() {
		System.out.print(s);
	}
}

class Exercise_6_1 {
	protected String s = "Hello!";
	void getS() {
		System.out.print(s);
	}
} ///:~



练习7:

package access;

public class Widget {
} ///:~
package lib.sixth;
import access.*;

public class Exercise_7 {
	public static void main(String[] args) {
		new Widget();
	}
} ///:~


练习8:

package lib.sixth;

class Connection {
	private Connection() {		
	}
	public static Connection makeConnection() {
		return new Connection();
	}
}

public class ConnectionManager {
	private static int i = -1;
	private static Connection[] con = {Connection.makeConnection(),
		Connection.makeConnection(), Connection.makeConnection()};
	public static Connection access() {
		if(i < con.length - 1) {
			i++;
			return con[i];
		}
		else
			return null;
	}
	public static void main(String[] args) {
		for(int i = 0; i < 4; i++) {
			System.out.println(access());
		}
	}
} /* Output:
[email protected]
[email protected]
[email protected]
null
*///:~


练习9:

-NULL-

 

-END-