日期:2014-05-20 浏览次数:20831 次
public static Object getInstance(String className,Object ... params){}调用方法后,会创建person对象 和初始化
public Class Person{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import java.lang.reflect.Field;
import java.lang.reflect.Type;
public class MyPerson {
/**
* 通过传入类的全限定名和成员属性值生成实例
* @param className
* @param params
* @return
*/
public static Object getInstance(String className, Object... params) {
Class<?> clazz = null;
Field[] fields = null;
Object o = null;
try {
clazz = Class.forName(className);
fields = clazz.getDeclaredFields(); // 取得类中的全部属性
o = clazz.newInstance();
// 传入的参数长度与生产的实例属性个数不一样
if (params.length != fields.length) {
return null;
}
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
Type type = fields[i].getType(); // 获得每个字段的类型
String in = params[i].getClass().getName();
Class<?> cIn = Class.forName(in); // 获得每个传入参数的类型
// 相对应的参数类型不一样
if (!type.equals(cIn)) {
return null;
}
fields[i].set(o, params[i]);
// Method method=clazz.getMethod("setName");
// method.invoke(o, params[0]);
// Method method2=clazz.getMethod("setAge");
// method2.invoke(o, params[1]);
// ((Person) o).setName((String)params[0]);
// ((Person) o).setAge((Integer) params[1]);
}
} catch (ClassNotFoundException e) {
System.out.println("找不到这个类...");
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return o;
}
public static void main(String[] args) {
// Person o = (Person) MyPerson.getInstance("Person", "haha", 20);
Person o = (Person) MyPerson.getInstance("Person", "haha", new Integer(20));
System.out.println(o.toString());
}
}
class Person {
private String name;
private Integer age; // 写成基本类型 int型时报错...
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {