利用反射获取类的属性名称

package com;

public class Test {


private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}

}

获取类

package com;

import java.lang.reflect.Field;

public class My {

public static void setAllComponentsName(Object f)

{
Field[] fields = f.getClass().getDeclaredFields();
for (int i = 0, len = fields.length; i < len; i++) {

try {
String varName = fields[i].getName();
boolean accessFlag = fields[i].isAccessible();
fields[i].setAccessible(true);
Object o = fields[i].get(f);
System.out.println("传入的对象中包含一个如下的属性:" + varName + " = " + o);
fields[i].setAccessible(accessFlag);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static void main(String[] args) {
setAllComponentsName(new Test());

}

}

你可能感兴趣的:(F#)