package test.demo;
public class Student {
private String id;
private String name;
private int age;
private Boolean isout;
public final String getId() {
return id;
}
public final void setId(String id) {
this.id = id;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
public final int getAge() {
return age;
}
public final void setAge(int age) {
this.age = age;
}
public final Boolean getIsout() {
return isout;
}
public final void setIsout(Boolean isout) {
this.isout = isout;
}
}
测试类:
package test.demo;
import java.lang.reflect.Field;
public class Test {
/**
* @param args
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
// TODO Auto-generated method stub
Student stu=new Student();
stu.setId("001");
stu.setAge(23);
stu.setName("tom");
stu.setIsout(new Boolean(true));
getObject(stu,"name");
}
public static void getObject(Object obj,String fieldName) throws IllegalArgumentException, IllegalAccessException{
Field [] fields=obj.getClass().getDeclaredFields();
for(Field field:fields){
field.setAccessible(true);//设置访问权限
System.out.println(field.get(obj).toString());
System.out.println(field.getName());
}
}
}