欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java基于反射得到对象属性值的方法

程序员文章站 2024-03-04 12:56:47
本文实例讲述了java基于反射得到对象属性值的方法。分享给大家供大家参考,具体如下: 通过反射机制得到对象中的属性和属性值 在对象中private没问题,在别的类中有时会...

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《java图片操作技巧汇总》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》及《java数据结构与算法教程》。

希望本文所述对大家java程序设计有所帮助。