java反射更改方法内容_Java反射根据不同的方法名称动态调用不同的方法(示例)...

4dadd57597ece75effa4525194d46450.png

更新时间: 2016年8月5日15:06:22提交: 静县

以下编辑器将带给您Java反射,以根据不同的方法名称动态调用不同的方法(示例). 编辑认为它很好,因此动态方法调用 为什么不安全,我现在将与您分享并提供参考. 让我们一起关注小编,看看

05136930A999C21236EFA98C2820761BBC0A25_size141_w765_h439.gif

列表页面的字段需求可以根据用户的偏好进行排序,因此每个用户的字段都对应一个不同的顺序(字段顺序存储在中),我们从中取出的值是一个对象,但是前端通过它使用的是ajax和json数组,因此它面临着对象到json的转换问题: 1.每个用户的字段顺序都不固定,并且代码也很难写. 2.根据用户字段的顺序,取值,如果由if判断动态方法调用 为什么不安全,则每个值然后调用不同的方法,如果有条件语句,则过多. 然后我看着反射.

模型类,与普通模型相同

261a1ef0b0fbed51b31086ce9aeddc6b.gif

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方法,省略。

}

测试班

9d70827c3a8e944a57892352bf768dfb.gif

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 list = new ArrayList();

// 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]));

}

}

}

可以根据获取的字段从对象中遍历样本以获得相应的值

856e619847aeaf5c0d734e01dab0b6e8.gif

以上方法是将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获取相应的值

上述Java反射根据不同的方法名称动态调用不同的方法(示例). 这就是我与您共享的所有内容. 我希望能给您参考,也希望您能支持该脚本主页.

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/tongxinshuyu/article-184792-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值