SoftDevoloper sd=null;
Class cls=null;
Method method=null;
try {
//实例化
sd = new SoftDevoloper(1, "java", 18);
//根据类名实例化类对象
cls = Class.forName("com.ibm.dto.SoftDevoloper");
//找到get方法
method=cls.getMethod("getDevName");
//调用invoke方法获得返回值
Object retVal = method.invoke(sd, null);
System.out.println("返回值是:"+retVal);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
/**
* 采用反射动态赋值
* @param from 原对象
* @param to 目标对象
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public void copy(Object from,Object to) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Class fromCls=from.getClass();
Class toCls=to.getClass();
Field[] field=fromCls.getDeclaredFields();
for(Field field2:field){
//所有属性名
String fieldName=field2.getName();
//get方法
String getMethodName="get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
//set方法
String setMethodName="set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
//根据方法名找到具体的方法
Method method=fromCls.getMethod(getMethodName, null);
//获得get方法所返回的值
Object objValue=method.invoke(from, null);
//返回值类型根据返回值类型来确定set方法中参数的类型
Class clsType=method.getReturnType();
Method setMethod=toCls.getMethod(setMethodName, clsType);
//将从from对象中取到值给to对象
setMethod.invoke(to, objValue);
}
}
(此处调用的是ArgDemo类)
/**
* 测试根据名称、参数来找方法
*/
public void testArg(){
try {
Class cls=Class.forName("com.ibm.test.ArgDemo");
Object obj=cls.newInstance();
String methodName="say";
//找到方法
Method method=cls.getDeclaredMethod(methodName, new Class[]{String.class,String.class,int.class});
//调用方法并依次赋参数
method.invoke(obj, new Object[]{"犀利哥","今年我要上春晚",7});
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Test test = new Test();
test.GetInstance();
test.testMethod();
test.testField();
test.testConst();
test.methodOperDemo();
SoftDevoloper sd1=new SoftDevoloper();
sd1.setDevName("bill");
sd1.setDevId(123);
sd1.setAge(10);
SoftDevoloper sd2 = new SoftDevoloper();
//将值从sd1复制到sd2中
try {
test.copy(sd1, sd2);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStac