public final class Class<T> extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement
1.getDeclaredFields
Returns an array of
Field objects reflecting all the fields declared by the class or interface represented by this Class object.(返回类或者接口的所有Field)package reflex; import java.lang.reflect.Field; public class TestClass { public static void main(String[] args) { Field[] fields = Person.class.getDeclaredFields(); System.out.println(fields.length); for(Field f : fields){ System.out.println(f.getName()); } } } class Person{ private String name; private String telephone; private int age; }
Returns an array containing
Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.(获得公开属性)package reflex; import java.lang.reflect.Field; public class TestClass2 { public static void main(String[] args) { //类的所有属性 Field[] fields = Person2.class.getFields(); System.out.println(fields.length); for(Field f : fields){ System.out.println(f.getName()); } } } class Person2{ private String name; String telephone;// protected int age; public String name2; public String telephone2; public int age2; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
浙公网安备 33010602011771号