java Class
public final class Class<T>
extends Object
implements Serializable, GenericDeclaration, Type, AnnotatedElement

1.getDeclaredFields

Field[] 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;
    
    
}

2.getFields

Field[] getFields()

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;
    }
}

 

 

 

posted on 2013-04-22 22:42  只愿软禁  阅读(158)  评论(0)    收藏  举报