日期:2014-05-20 浏览次数:20804 次
Book:
private String name;
private String author;
private Integer number;
private Float length;
private Float width;
private Boolean isBorrowed;
public Book(String name, String author, Integer number, Float length,
Float width, Boolean isBorrowed) {
this.name = name;
this.author = author;
this.number = number;
this.length = length;
this.width = width;
this.isBorrowed = isBorrowed;
}
/**
* @param classType 要生成的Class对象
* @param obj 生成对象所要带的参数
*/
public Object getObject(Class classType, Object[] obj){
// ……
// 判断池中是否已存在挂起的对象
// ……
// 如果池中没有挂起的对象,则需要新建对象
Class[] clazzs = new Class[obj.length];
for(int i = 0; i < obj.length; i++){
clazzs[i] = obj[i].getClass();
}
Constructor cons = classType.getConstructor(clazzs);
return cons.newInstance(obj);
}
Book book = (Book)ObjectPool.getObject(Book.class, new Object[]{"helloworld", "smith", 123, 23.5f, 18.0f, true});