获取指定类下的信息:所有方法和属性
 
public class DumpClassInfo {

    public static void main(String[] args) throws Exception{
        //Reflection API的基本作用
        Class<?> classtype = Class.forName("my.reflect.Customer");
        Method[] methods = classtype.getDeclaredMethods();
        //获取指定类下的所有方法,包含私有方法。(运行时所有方法)
        for(Method method : methods){
            System.out.println(method);
        }
        Field[] fields = classtype.getDeclaredFields();
        //获取指定类下的所有属性,包含私有属性。(运行时所有属性)
        for(Field field : fields){
            System.out.println(field);
        }
    }
}
class Customer{

    private long id;
    private String name;
    private int age;
     
    public Customer(){}
     
    public Customer(String name, int age){
        this.name = name;
        this.age = age;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

 

 
以上程序输出结果:
 
public void my.reflect.Customer.setId(long)
public int my.reflect.Customer.getAge()
public void my.reflect.Customer.setAge(int)
public java.lang.String my.reflect.Customer.getName()
public long my.reflect.Customer.getId()
public void my.reflect.Customer.setName(java.lang.String)
private long my.reflect.Customer.id
private java.lang.String my.reflect.Customer.name
private int my.reflect.Customer.age