http://hi.baidu.com/wangenglishklx/item/4633fce5e1b967138d3ea82b
import java.lang.reflect.Field;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
Field[] customerProperties = Customer.class.getDeclaredFields();
System.out.println("The fields are : "+Arrays.toString(customerProperties));
for(int i = 0; i < customerProperties.length; i++) {
System.out.print(customerProperties[i].getName()+" ");
}
}
bean 属性名不能用private, 不加修饰符就可以了
import java.lang.reflect.Field;
Field[] fields = TypeAVCReply.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Object value = fields[i].get(avcReply);
if(value == null){
fields[i].set(avcReply, "");
}
}
本文通过两个示例展示了如何使用Java反射API获取类的所有字段,并对字段进行操作。第一个示例列出`Customer`类的所有字段名称;第二个示例则演示了如何遍历`TypeAVCReply`类的所有字段,并为null值设置默认字符串。
1008

被折叠的 条评论
为什么被折叠?



