当前位置: 移动技术网 > IT编程>开发语言>Java > Java反射根据不同方法名动态调用不同的方法(实例)

Java反射根据不同方法名动态调用不同的方法(实例)

2019年07月22日  | 移动技术网IT编程  | 我要评论

大军师司马懿之龙吟虎啸,网红警察睡粉丝十几人遭举报,胡文丽

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反射根据不同方法名动态调用不同的方法(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网