⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionmanager.java

📁 java编程思想第四版习题答案
💻 JAVA
字号:
// access/ConnectionManager.java
// TIJ4 Chapter Access, Exercise 8, page 233
/* Following the form of the example Lunch.java, create a class called
* ConnectionManager that manages a fixed array of Connection objects. The client
* programmer must not be able to explicitly create Connection objects, but can
* only get them via a static method in ConnectionManager. When ConnectionManager
* runs out of objects, it returns a null reference. Test the classes in main(). */
package access;

class Connection {
	private static int count = 0;
	private int i = 0;
	private Connection() { System.out.println("Connection()");}
	// Allow creation via static method:
	static Connection makeConnection() {
		count++;
		return new Connection();
	}
	public static int howMany() { return count; }
	public String toString() {
		return ("Connection " + count);
	}
}

public class ConnectionManager {
	static int howManyLeft = 3;
	static Connection[] ca = new Connection[3];
	{
	for(Connection x : ca)
		x = Connection.makeConnection();
	}
	public static Connection getConnection() {
		if(howManyLeft > 0)
			return ca[--howManyLeft];
		else {
			System.out.println("No more connections");
			return null;
		}
	}
	public static void main(String[] args) {
		ConnectionManager cm = new ConnectionManager();
		System.out.println(cm.howManyLeft);
		cm.getConnection();
		System.out.println(howManyLeft);
		cm.getConnection();
		System.out.println(howManyLeft);
		cm.getConnection();
		System.out.println(cm.getConnection());
		System.out.println(howManyLeft);
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -