Method方法学习

发布时间:2024-11-23 20:39

学习方法论,如结构化学习 #生活技巧# #学习技巧# #跨学科学习法#

最新推荐文章于 2021-08-09 22:37:36 发布

胖鹅68 于 2013-12-04 10:10:06 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

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

}

}

}

网址:Method方法学习 https://www.yuejiaxmz.com/news/view/218778

相关内容

springmvc 使用REST 出现此种错误:Request method 'PUT' not supported
高效的学习方法一费曼学习法
【Ruby报错已解决】NoMethodError: undefined method `each‘ for nil:NilClass
高效学习的四大方法
关于提高学习效率的英语作文(通用5篇)
有哪些相见恨晚的高效学习方法?
如何才能高效学习,99%的人不知道的高效学习法
学习方法,学习方法有哪些
“方法”字的解释
学习方法

随便看看