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

📄 classtypecreator22.java

📁 JAVA编程思想第四版英文原版习题答案. pdf原版的
💻 JAVA
字号:
// generics/ClassTypeCreator22.java
// TIJ4 Chapter Generics, Exercise 22, page 667
/* Use a type tag along with reflection to create a method that uses the 
* argument version of newInstance() to create an object of a class with
* a constructor that has arguments.
*/
import java.util.*;
import java.lang.reflect.*;
import static org.greggordon.tools.Print.*;

class Building {}
class House extends Building {
	private String location;
	private Integer area;
	public House() { location = null; }
	public House(Integer area) { this.area = area; }
	public House(String location) {
		this.location = location;
	}
	public House(String location, Integer area) {
		this.location = location;
		this.area = area;
	}
	public String toString() {
		return "House" + ((location == null) ? "" :  " in " + location) +
			((area != null) ? (", "+ area + " sqft") : "");
	}
}

public class ClassTypeCreator22<T> {
	Class<?> kind;
	public ClassTypeCreator22(Class<?> kind) {
		this.kind = kind;
	}
	public Object createNew(String typename) 
		throws 	IllegalAccessException, 
			InstantiationException, 
			ClassNotFoundException {
		return Class.forName(typename).newInstance();
	}	
	/** To create instance of typename with constructors taking arguments args: */
	public Object createNew(String typename, Object... args) 
		throws 	IllegalAccessException, 
			InstantiationException, 
			ClassNotFoundException,
			NoSuchMethodException,
			InvocationTargetException {
		switch(args.length) {
		case 1 : return Class.forName(typename).getConstructor(args[0].getClass()).newInstance(args[0]);
		case 2 : return Class.forName(typename).getConstructor(args[0].getClass(), args[1].getClass()).
			newInstance(args[0], args[1]);
		}
		return null;
	}
	public static void main(String[] args) {
		ClassTypeCreator22<Building> ctcb = new ClassTypeCreator22<Building>(Building.class);
		ClassTypeCreator22<House> ctch = new ClassTypeCreator22<House>(House.class);
		try {
			Building b = (Building)ctcb.createNew("Building");
			// To show we can access and print House constructors:
			println("House constructors:");
			Constructor[] ctors = House.class.getConstructors();
			for(Constructor ctor : ctors) println(ctor);
			// create 3 new House objects:
			House h = (House)ctch.createNew("House", "Hawaii");
			House h2 = (House)ctch.createNew("House", 3000);
			House h3 = (House)ctch.createNew("House", "Manila", 5000);
			println("Constructed House objects:");
			println(h);
			println(h2);
			println(h3);
		} catch(IllegalAccessException e) {
			println("IllegalAccessException in main");
		} catch(InstantiationException e) {
			println("InstantiationException in main");
		} catch(ClassNotFoundException e) {
			println("ClassNotFoundException in main");
		} catch(NoSuchMethodException e) {
			println("NoSuchMethodException in main");
		} catch(InvocationTargetException e) {
			println("InvocationTargetException in main");
		}		
	}
}

⌨️ 快捷键说明

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