使用反射,根据属性名前缀+编号获取值、设置值

  1 /**
  2  * 反射
  3  * 根据属性名前缀+编号获取值、设置值
  4  * 如item + 12, mark +5 
  5  */
  6 public class HandleProperty {
  7 
  8     /**
  9      * 取值
 10      * @param object
 11      * @param prefix
 12      * @param i
 13      * @return
 14      */
 15     public static Object getProperty(Object object, String prefix, int i) {
 16         try {
 17             Class<?> objectClass = object.getClass();
 18 
 19             // 根据前缀和索引获取属性值
 20             String propertyName = prefix + i;
 21             String getterMethodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
 22             Method getterMethod = objectClass.getMethod(getterMethodName);
 23             return getterMethod.invoke(object);
 24         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
 25             e.printStackTrace();
 26             return null;
 27         }
 28     }
 29 
 30     /**
 31      * 设置参数值类型为String
 32      * @param object
 33      * @param prefix
 34      * @param i
 35      * @param value
 36      * @return
 37      */
 38     public static Object setProperty(Object object, String prefix, String i, String value) {
 39         try {
 40             Class<?> objectClass = object.getClass();
 41 
 42             // 根据前缀和索引构建属性名
 43             String propertyName = prefix + i;
 44             String setterMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
 45 
 46             // 获取属性的 setter 方法
 47             Method setterMethod = objectClass.getMethod(setterMethodName, String.class);
 48 
 49             // 调用 setter 方法设置属性值
 50             setterMethod.invoke(object, value);
 51             return object;
 52         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
 53             e.printStackTrace();
 54             return null;
 55         }
 56     }
 57 
 58     /**
 59      * 设置参数值类型为int
 60      * @param object
 61      * @param prefix
 62      * @param i
 63      * @param value
 64      * @return
 65      */
 66     public static Object setProperty(Object object, String prefix, String i, int value) {
 67         try {
 68             Class<?> objectClass = object.getClass();
 69 
 70             // 根据前缀和索引构建属性名
 71             String propertyName = prefix + i;
 72             String setterMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
 73 
 74             // 获取属性的 setter 方法
 75             Method setterMethod = objectClass.getMethod(setterMethodName, int.class);
 76 
 77             // 调用 setter 方法设置属性值
 78             setterMethod.invoke(object, value);
 79             return object;
 80         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
 81             e.printStackTrace();
 82             return null;
 83         }
 84     }
 85 
 86     /**
 87      * 设置参数值类型为Long
 88      * @param object
 89      * @param prefix
 90      * @param i
 91      * @param value
 92      * @return
 93      */
 94     public static Object setProperty(Object object, String prefix, String i, Long value) {
 95         try {
 96             Class<?> objectClass = object.getClass();
 97 
 98             // 根据前缀和索引构建属性名
 99             String propertyName = prefix + i;
100             String setterMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
101 
102             // 获取属性的 setter 方法
103             Method setterMethod = objectClass.getMethod(setterMethodName, Long.class);
104 
105             // 调用 setter 方法设置属性值
106             setterMethod.invoke(object, value);
107             return object;
108         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
109             e.printStackTrace();
110             return null;
111         }
112     }
113 
114 
115     public static void main(String[] args) {
116         User getUser = new User();
117 
118         // 使用前缀 "item" 和索引 2 获取属性值
119         Object itemPropertyO = getProperty(getUser, "item", 2);
120         Long itemProperty = (Long)itemPropertyO;
121         System.out.println("Item Property: " + itemProperty);
122 
123         // 使用前缀 "mark" 和索引 1 获取属性值
124         Object markPropertyO = getProperty(getUser, "mark", 1);
125         String markProperty = (String)markPropertyO;
126         System.out.println("Mark Property: " + markProperty);
127 
128 
129 
130         User setUser = new User();
131 
132         // 使用前缀 "item" 和索引 2 设置属性值为字符串
133         setProperty(setUser, "item", "2", "NewStringValue");
134         System.out.println("Item Property After Setting (String): " + setUser.getItem2());
135 
136         // 使用前缀 "item" 和索引 2 设置属性值为整数
137         setProperty(setUser, "item", "2", 42);
138         System.out.println("Item Property After Setting (int): " + setUser.getItem2());
139 
140         // 使用前缀 "mark" 和索引 1 设置属性值为整数
141         setProperty(setUser, "mark", "1", 123);
142         System.out.println("Mark Property After Setting (int): " + setUser.getMark1());
143     }
144 }

 

posted @ 2023-09-20 15:14  面向bug编程  阅读(13)  评论(0编辑  收藏  举报