Java反射根據(jù)不同方法名動態(tài)調(diào)用不同的方法(實例)
list頁面的字段要求可以根據(jù)用戶的喜好進(jìn)行排序,所以每個用戶的字段都對應(yīng)著不同的順序(字段順序存數(shù)據(jù)庫),我們從數(shù)據(jù)庫里取出來的值是對象,但是前臺傳值是用的ajax和json array,所以就面臨著一個對象到j(luò)son的轉(zhuǎn)換問題:1. 每個用戶的字段順序不固定,代碼不能寫死, 2. 根據(jù)用戶字段順序去取值,如果用if判斷每個值然后調(diào)用不同的方法,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])); } } }
樣就可以根據(jù)數(shù)據(jù)庫獲取的字段遍歷從對象去取相應(yīng)的值了
上面那個方法是要給list添加get方法名,才能根據(jù)相應(yīng)的get方法名去獲取值,如果前臺傳過來的只是一個屬性名,那我們還要轉(zhuǎn)換成相應(yīng)的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獲取對應(yīng)的value值了
以上這篇Java反射根據(jù)不同方法名動態(tài)調(diào)用不同的方法(實例)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問題解決
這篇文章主要介紹了java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11springboot中JSONObject遍歷并替換部分json值
這篇文章主要介紹了springboot中JSONObject遍歷并替換部分json值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11springboot處理url中帶斜杠/\字符的參數(shù)報400問題
這篇文章主要介紹了springboot處理url中帶斜杠/\字符的參數(shù)報400問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Spring @value和@PropertySource注解使用方法解析
這篇文章主要介紹了Spring @value和@PropertySource注解使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
這篇文章主要介紹了SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07