package hb.util;
import java.util.Date;
public class Person {
private String name;
private String password;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String print(Person person){
System.out.println("姓名:" + person.name + "密码:" + person.password + "生日" + person.birthday);
return "姓名:" + person.name + "密码:" + person.password + "生日" + person.birthday;
}
}
package hb.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ParamObject {
private Object instance;
private String method;
public ParamObject(Object instance,String method){
this.instance = instance;
this.method = method;
}
public Object execute() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Class cls = instance.getClass();
Method md = cls.getMethod(this.method, null);
return md.invoke(instance, null);
}
}
package hb.util;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
public class TestParamObject {
public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Person person = new Person();
person.setName("name");
person.setPassword("person");
person.setBirthday(new Date());
ParamObject param = new ParamObject(person, "getPassword");
if(param.execute() instanceof String){
System.out.println("返回的结果是String 类型");
System.out.println(param.execute());
}else{
System.out.println("返回的结果不是String 类型");
}
ParamObject param1 = new ParamObject(person, "getBirthday");
if(param1.execute() instanceof String){
System.out.println("返回的结果是String 类型");
System.out.println(param1.execute());
}else{
System.out.println("返回的结果不是String 类型");
System.out.println(param1.execute());
}
}
}
package hb.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
public class TestMethodInvoke {
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Person person = new Person();
person.setName("name");
person.setPassword("person");
person.setBirthday(new Date());
Method md = person.getClass().getMethod("print", Person.class);
md.invoke(person, person);
if(md.invoke( person, person) instanceof String){
System.out.println("返回的是String 类型");
}
}
}