Method方法学习

 

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 类型");
			
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值