实体类
package com.sunwayworld.reflect;
public class TetsObj {
public TetsObj() {
super();
}
public TetsObj(String desc1, String desc2, String desc3, String desc4, String desc5, String desc6) {
super();
this.desc1 = desc1;
this.desc2 = desc2;
this.desc3 = desc3;
this.desc4 = desc4;
this.desc5 = desc5;
this.desc6 = desc6;
}
private String desc1;
private String desc2;
private String desc3;
private String desc4;
private String desc5;
private String desc6;
public String getDesc1() {
return desc1;
}
public void setDesc1(String desc1) {
this.desc1 = desc1;
}
public String getDesc2() {
return desc2;
}
public void setDesc2(String desc2) {
this.desc2 = desc2;
}
public String getDesc3() {
return desc3;
}
public void setDesc3(String desc3) {
this.desc3 = desc3;
}
public String getDesc4() {
return desc4;
}
public void setDesc4(String desc4) {
this.desc4 = desc4;
}
public String getDesc5() {
return desc5;
}
public void setDesc5(String desc5) {
this.desc5 = desc5;
}
public String getDesc6() {
return desc6;
}
public void setDesc6(String desc6) {
this.desc6 = desc6;
}
}
测试类
package com.sunwayworld.reflect;
import java.lang.reflect.Field;
public class TestreFlect {
public static void main(String[] args) {
TetsObj tetsObj = new TetsObj("desc1", "desc2", "desc3", "desc4", "desc5", "desc6");
Class<?> clazz = tetsObj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true); //setAccessible(true);得作用就是让我们在用反射时访问私有变量
String fieldName = field.getName();
String value = "";
try {
value = field.get(tetsObj).toString();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("属性:"+fieldName+"\t值:"+value);
}
}
}
结果展示
得到所有属性及其对应得值。