《Java编程思想》第6章 练习题

源码地址:https://github.com/yangxian1229/ThinkingInJava
练习1:略。
练习2:将6.12节中的代码片段改写为完整的程序,并校验实际所发生的冲突。
练习3:创建两个包,它们都包含一个相同的类,该类有一个debug()方法。第一个版本显示发送给控制台的String参数,而第二个版本什么都不做。使用静态import语句将该类导入到一个测试程序中,并示范条件编译效果。
练习4:展示protected方法具有包访问权限,但不是public。
练习5:创建一个带有public,private,protected和包访问权限域以及方法成员的类。创建一个该类的对象,看看在你试图调用所有类成员时,会得到什么类型的编译信息。请注意,处于同一个目录中的所有类都是默认包的一部分。
练习6:略。
练习7:略。
练习8:效仿示例Lunch.java的形式,创建一个名为ConnectionManager的类,该类管理一个元素为Connection对象的固定数组。客户端程序猿不能直接创建Connection对象,而只能通过ConnectionManager中的某个static方法来获取他们。当ConnectionManager之中不再有对象时,它会返回null引用。在main()之中检测这些类。

package ch6_1;

public class Connection {
	private static int counter = 0;
	private int id = counter ++;
	Connection(){}
	public String toString(){
		return "Connection "+id;
	}
	public void doSomething(){}
}///:~
package ch6_1;

import net.mindview.util.New;

public class ConnectionManager {
	private static Connection[] pool = new Connection[10];
	private static int counter = 0;
	static {
		for(int i=0;i<pool.length;i++)
			pool[i] = new Connection();
	}
	// Very simple -- just hands out each one once:
	public static Connection getConnection(){
		if(counter < pool.length)
			return pool[counter++];
		return null;
	}
}///:~

package ch6;

import ch6_1.Connection;
import ch6_1.ConnectionManager;

public class E08 {

	public static void main(String[] args) {
		Connection c = ConnectionManager.getConnection();
		while(c != null){
			System.out.println(c);
			c.doSomething();
			c = ConnectionManager.getConnection();
		}
	}
}/* Output:
Connection 0
Connection 1
Connection 2
Connection 3
Connection 4
Connection 5
Connection 6
Connection 7
Connection 8
Connection 9
*/

The Connection class identifies each Connection object with a static int called counter, which produces the identifier as part of its toString( ) representation. Connection also has a doSomething( ) method to indicate the task for which you created the Connection object.
Note that the constructor for Connection has package ch6_1; it is unavailable outside of this package, so the client programmer cannot access it to make instances of Connection directly. The only way to get Connection objects is through the ConnectionManager.
ConnectionManager initializes a static array of objects inside the static clause that is called only once when the class loads.

Here is a more sophisticated connection manager that allows the client programmer to “check in” a connection when finished with it:

package ch6_1;

public class Connection2 {
	private static int counter = 0;
	private int id = counter++;
	Connection2() {}
	public String toString(){
		return "Connection2 "+id;
	}
	public void doSomething(){}
	public void checkIn(){
		ConnectionManager2.checkIn(this);
	}

}///:~

package ch6_1;

public class ConnectionManager2 {
	private static Connection2[] pool = new Connection2[10];
	static {
		for(int i=0;i<pool.length;i++)
			pool[i] = new Connection2();
	}
	//Produce the first available connection:
	public static Connection2 getConnection2(){
		for(int i=0;i<pool.length;i++)
			if(pool[i] != null){
				Connection2 c2 = pool[i];
				pool[i] = null;//Indicates "in use"
				return c2;
			}
		return null; //None left
	}
	public static void checkIn(Connection2 c2){
		for(int i=0;i<pool.length;i++)
			if(pool[i] == null){
				pool[i] = c2; //Check it back in
				return;
			}
	}
}///:~

package ch6;

import ch6_1.Connection2;
import ch6_1.ConnectionManager2;

public class E08_2 {

	public static void main(String[] args) {
		Connection2[] ca = new Connection2[10];
		//Use up all the connections
		for(int i=0;i<10;i++)
			ca[i] = ConnectionManager2.getConnection2();
		//Should produce "null" since there are no more connections:
		System.out.println(ConnectionManager2.getConnection2());
		//Return connections, then get them out:
		for(int i=0;i<5;i++){
			ca[i].checkIn();
			Connection2 c2 = ConnectionManager2.getConnection2();
			System.out.println(c2);
			c2.doSomething();
			//c2.checkIn();
		}
	}
}/* Output:
null
Connection2 0
Connection2 1
Connection2 2
Connection2 3
Connection2 4
*/

When a Connection is checked out, its slot in pool is set to null. When the client programmer is done with the connection, checkIn( ) returns it to the connection pool by assigning it to a null slot in pool.
However, there are all kinds of potential problems with this approach. What if a client checks in a Connection and then continues to use it, or checks in more than once? We address the problem of the connection pool more thoroughly in Thinking in Patterns with Java (available from www.MindView.net)

猜你喜欢

转载自blog.csdn.net/lanzijingshizi/article/details/84037203