关闭

Method方法学习

216人阅读 评论(0) 收藏 举报

 

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);//得到字符串为this.method的方法,并且没有参数
		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());
		//得到方法名字是print,参数是Person类型的方法
		Method md = person.getClass().getMethod("print", Person.class);
		md.invoke(person, person);//执行第一个person对象的的print方法,传递第二个person作为参数
		if(md.invoke( person, person) instanceof String){
			System.out.println("返回的是String 类型");
			
		}
	}

}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:325593次
    • 积分:8908
    • 等级:
    • 排名:第2229名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论