java用反射得到对象的属性值

30 篇文章 0 订阅

 
通过反射机制得到对象中的属性和属性值 在对象中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;
 }

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值