国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java反射根據不同方法名動態調用不同的方法(實例)

2019-11-26 14:00:33
字體:
來源:轉載
供稿:網友

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反射根據不同方法名動態調用不同的方法(實例)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海门市| 历史| 七台河市| 博白县| 时尚| 内黄县| 成都市| 濮阳市| 德保县| 五常市| 横峰县| 临湘市| 柞水县| 呼伦贝尔市| 娱乐| 独山县| 轮台县| 涪陵区| 共和县| 吴忠市| 三亚市| 拉孜县| 武隆县| 丹阳市| 揭西县| 同江市| 通海县| 永善县| 和林格尔县| 定南县| 郧西县| 两当县| 永和县| 宣恩县| 金沙县| 延边| 诸城市| 乌兰浩特市| 河东区| 鄱阳县| 普宁市|