首頁  >  文章  >  Java  >  java基於反射得到物件屬性值的範例程式碼分享

java基於反射得到物件屬性值的範例程式碼分享

黄舟
黄舟原創
2017-03-27 10:09:051525瀏覽

這篇文章主要介紹了java基於反射得到物件屬性值的方法,結合實例形式分析了java基於反射獲取物件屬性值的相關實現方法與操作技巧,需要的朋友可以參考下

本文實例講述了java基於反射得到物件屬性值的方法。分享給大家供大家參考,具體如下:

透過反射機制得到物件中的屬性和屬性值 在物件中private沒問題,在別的類別中有時會報異常。下面的例子是在本物件中

/**
* Engine entity. @author MyEclipse Persistence Tools
*/
public class Engine implements java.io.Serializable {
 // Fields
 private Long engineId;
 private String engineName;
 private String engineType;
 // Constructors
 /** default constructor */
 public Engine() {
 }
 /** minimal constructor */
 public Engine(Long engineId) {
 this.engineId = engineId;
 }
 public Engine(String engineName, String engineType) {
 this.engineName = engineName;
 this.engineType = engineType;
 }
 /** full constructor */
 public Engine(Long engineId, String engineName, String engineType) {
 this.engineId = engineId;
 this.engineName = engineName;
 this.engineType = engineType;
 }
 // Property accessors
 public Long getEngineId() {
 return this.engineId;
 }
 public void setEngineId(Long engineId) {
 this.engineId = engineId;
 }
 public String getEngineName() {
 return this.engineName;
 }
 public void setEngineName(String engineName) {
 this.engineName = engineName;
 }
 public String getEngineType() {
 return this.engineType;
 }
 public void setEngineType(String engineType) {
 this.engineType = engineType;
 }
public static void main(String[] args) throws Exception {
 Engine m = new Engine("汽车","发动机");
 Class clazz = m.getClass();
 Field[] f = clazz.getDeclaredFields();
 String[] name = field2Name(f);
 Object[] value = field2Value(f, m);
 showNameAndValue(name, value);
 }
 public static void showNameAndValue(String[] name, Object[] value) {
 for (int i = 0; i < name.length; i++) {
  System.out.println("--" + i + "--");
  System.out.println("name:" + name[i]);
  System.out.println("Value:" + value[i]);
  System.out.println("--   --");
 }
 }
 public static String[] field2Name(Field[] f) {
 String[] name = new String[f.length];
 for (int i = 0; i < f.length; i++) {
  name[i] = f[i].getName();
 }
 return name;
 }
 public static Object[] field2Value(Field[] f, Object o) throws Exception {
 Object[] value = new Object[f.length];
 for (int i = 0; i < f.length; i++) {
  value[i] = f[i].get(o);
 }
 return value;
 }
}

如果想呼叫private的屬性的屬性值,則要呼叫他的get方法了具體做法是:

把field2Value方法改為如下方式,這樣在別的類別中也可以得到private的屬性值了

public static Object[] field2Value(Field[] fields, Object o) throws Exception {
 Object[] values = new Object[fields.length];
 Class classType = o.getClass();
 for (int i = 0; i < fields.length; i++) {
  String fieldName = fields[i].getName();
  String firstLetter = fieldName.substring(0, 1).toUpperCase();
  String getMethodName = "get" + firstLetter + fieldName.substring(1);
  Method getMethod = classType.getMethod(getMethodName, new Class[] {});
  values[i] = getMethod.invoke(o, new Object[] {});
 }
 return values;
}

以上是java基於反射得到物件屬性值的範例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn