获取javaBean的属性名属性值属性类型
程序员文章站
2022-04-02 16:43:55
...
package com.example; import java.lang.reflect.Field; /** * 通用的单据主表 * * @author yi.zhe * @time 2014-9-10 下午6:44:17 */ public class BillMaster extends BaseTableBean { public String CompanyID = "123"; Integer id; Long BillNo; public static void main(String[] args) throws Exception { BillMaster a = new BillMaster(); a.iniContentValues(); } public BillMaster() { } public void iniContentValues() { Field[] fields = getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; f.setAccessible(true); try { if (f.getType().equals(java.lang.Integer.class)) { if (f.get(this) == null) { System.out.println(f.getName() + ":" + 0); } else { System.out.println(f.getName() + ":" + (int) f.get(this)); } } if (f.getType().equals(java.lang.Long.class)) { if (null == f.get(this)) { System.out.println(f.getName() + ":" + 0); } else { System.out.println(f.getName() + ":" + (long) f.get(this)); } } if (f.getType().equals(java.lang.String.class)) { System.out.println(f.getName() + ":" + (String) f.get(this)); } } catch (IllegalAccessException e) { e.printStackTrace(); } } } }