Method方法学习
来源:互联网 发布:js点击按钮显示输入框 编辑:程序博客网 时间:2024/04/29 23:55
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 类型");}}}
- Method方法学习
- Ruby学习笔记(5) - 方法(Method)
- 设计模式学习-Factory Method(工厂方法)
- 设计模式学习-Template Method(模板方法)
- JavaScript学习笔记(二十九) method()方法
- method方法
- 方法(method)
- 方法Method
- method方法
- 码农小汪-spring框架学习之3-spring Method injection(方法注入 ) Lookup method injection
- 《Pro Spring》学习笔记之Method Replacement(方法替换)
- 设计模式学习笔记:factory method(工厂方法)
- 【设计模式】学习笔记11:模板方法(Template Method)
- 设计模式学习笔记--模板方法(Template Method)模式
- 设计模式学习之 Factory method (工厂方法)
- JVM学习笔记-本地方法栈(Native Method Stacks)
- 再谈机器学习中的归一化方法(Normalization Method)
- 学习"JAVA深入研究——Method的Invoke方法。"
- MySql中测试GUID 与Int自增主键 性能对比 总结适用场景
- eclipse svn和TortoiseSVN同时操作文件导致eclipse不识别
- sqlserver2008时间查询
- 超级链接和事件同事存在处理
- 输入法问题处理
- Method方法学习
- myeclipse8.6 安装SVN出的问题
- 硬盘分区格式化后数据如何恢复
- 常用视频格式与视频编解码标准介绍
- 拓展javascript默认对象
- struts2 重定向
- struts2 参数传递
- hibernate中get方法和load方法的根本区别
- Linux 查看CPU,内存,硬盘