Java反射根据不同方法名动态调用不同的方法(实例)
作者:jingxian 时间:2022-05-08 00:51:47
list页面的字段要求可以根据用户的喜好进行排序,所以每个用户的字段都对应着不同的顺序(字段顺序存数据库),我们从数据库里取出来的值是对象,但是前台传值是用的ajax和json array,所以就面临着一个对象到json的转换问题:1. 每个用户的字段顺序不固定,代码不能写死, 2. 根据用户字段顺序去取值,如果用if判断每个值然后调用不同的方法,if条件语句太多。然后就看了下反射。
Model 类,跟正常model一样
public class Person {
private String name;
private int age;
private String address;
private String phoneNumber;
private String sex;
public String getName() {
return name;
}
// 以下是get 和set方法,省略。
}
测试类
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Test {
// init person object.
private Person initPerson() {
Person p = new Person();
p.setName("name");
p.setAge(21);
p.setAddress("this is my addrss");
p.setPhoneNumber("12312312312");
p.setSex("f");
return p;
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Test test = new Test();
Person p = test.initPerson();
List<String> list = new ArrayList<String>();
// Add all get method.
// There is no ‘()' of methods name.
list.add("getName");
list.add("getAge");
list.add("getAddress");
list.add("getPhoneNumber");
list.add("getSex");
for (String str : list) {
// Get method instance. first param is method name and second param is param type.
// Because Java exits the same method of different params, only method name and param type can confirm a method.
Method method = p.getClass().getMethod(str, new Class[0]);
// First param of invoke method is the object who calls this method.
// Second param is the param.
System.out.println(str + "(): Get Value is " + method.invoke(p, new Object[0]));
}
}
}
样就可以根据数据库获取的字段遍历从对象去取相应的值了
上面那个方法是要给list添加get方法名,才能根据相应的get方法名去获取值,如果前台传过来的只是一个属性名,那我们还要转换成相应的get方法,麻烦。
public static void getValueByProperty(Person p, String propertyName) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
// get property by the argument propertyName.
PropertyDescriptor pd = new PropertyDescriptor(propertyName, p.getClass());
Method method = pd.getReadMethod();
Object o = method.invoke(p);
System.out.println("propertyName: " + propertyName + "\t value is: " + o);
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException {
Test test = new Test();
Person p = test.initPerson();
// get all properties.
Field[] fields = p.getClass().getDeclaredFields();
for (Field field : fields) {
getValueByProperty(p, field.getName());
}
}
这样就能直接通过传过来的propertyName获取对应的value值了
标签:Java,反射,动态调用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++ 实现球迷 今日头条面试题
2022-07-08 11:03:24
Java语言实现数据结构栈代码详解
2021-12-09 16:15:05
全面了解Java中Native关键字的作用
2022-07-13 02:01:09
![](https://img.aspxhome.com/file/2023/9/88199_0s.png)
Java集合定义与用法实例总结【Set、List与Map】
2023-11-21 08:26:40
![](https://img.aspxhome.com/file/2023/8/59588_0s.png)
java线程并发控制同步工具CountDownLatch
2022-09-02 12:18:06
![](https://img.aspxhome.com/file/2023/1/75461_0s.jpg)
Android实现qq列表式的分类悬浮提示
2022-09-01 09:23:44
![](https://img.aspxhome.com/file/2023/6/138726_0s.gif)
一文教你如何使用原生的Feign
2023-01-12 22:12:58
![](https://img.aspxhome.com/file/2023/4/113384_0s.png)
Android Studio中Logcat写入和查看日志
2021-08-05 12:37:18
![](https://img.aspxhome.com/file/2023/9/106019_0s.png)
Android实现网页图片浏览功能
2023-09-05 00:16:09
java自定义切面增强方式(关于自定义注解aop)
2022-05-30 15:16:58
C#延时函数的使用说明
2023-10-11 19:58:34
Java设计模式之java命令模式详解
2023-11-13 16:00:36
![](https://img.aspxhome.com/file/2023/5/59685_0s.png)
Android编程实现横竖屏切换时不销毁当前activity和锁定屏幕的方法
2022-03-08 17:38:23
SpringBoot定时任务设计之时间轮案例原理详解
2023-04-24 00:48:36
![](https://img.aspxhome.com/file/2023/0/62450_0s.png)
Java创建表格实例详解 <font color=red>原创</font>
2022-12-22 04:59:13
简述Java编程之关系操作符
2021-08-07 09:44:23
深入理解java中的重载和覆盖
2023-01-05 00:24:22
IDEA 2021.3 使用及idea2021.3.1激活使用方法
2021-06-06 03:49:38
![](https://img.aspxhome.com/file/2023/0/73030_0s.jpg)
Java如何通过枚举实现有限状态机
2021-08-05 04:21:05
Android监听Home键和Back键的区别介绍
2022-09-11 11:21:03