导航

Field类使用以及getDeclaredFields方法

Posted on 2012-08-01 16:46  曙光城邦  阅读(12666)  评论(0编辑  收藏  举报
public interface IStudent {
	int cardId=1024;
}
public class Person {
	String personName;
}

 

public class Student extends Person implements IStudent{
    public static final int NUM=3;
    private String name;
    /*package*/ int age;
    protected Object obj;
    public ArrayList<Integer> ids;
    
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    
    /**getDeclaredFields()方法说明
     * Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. 
     * This includes public, protected, default (package) access, and private fields, but excludes inherited fields. 
     * The elements in the array returned are not sorted and are not in any particular order.
     *  This method returns an array of length 0 if the class or interface declares no fields, 
     *  or if this Class object represents a primitive type, an array class, or void. 
     *  
     *  返回一个Field对象数组,它是通过反射所有在类或接口中申明的字段得到的,代表这个类字节码对象。
     *  返回的Field包含public protected package private修饰的字段,即所有访问权限的字段。但不包括继承或实现的接口的字段。
     *  Field数组中的元素是无序的。
     *  当类或接口中没有任何字段,则返回数组的length为0.
     *  当为基本类型或者是数组类型,void类型返回的数组长度也为0.
     *  
     *  
     */
    public static void main(String[] args) {
        //Student字节码
        Class<Student> clazz = Student.class;
        Field[] fields = clazz.getDeclaredFields();
        printFields(fields);
        /*        输出结果:
         *         DeclaredFields.length=5
                public static final int NUM 
                private String name 
                 int age 
                protected Object obj 
                public ArrayList ids */
        
        //IStudent字节码
        Class<IStudent> clazzIStudent = IStudent.class;
        Field[] fields2 = clazzIStudent.getDeclaredFields();
        printFields(fields2);
        
        /*        
         *         DeclaredFields.length=1
                public static final int cardId
                */
        
        
        
        //基本类型字节码
        Class clazzInteger = void.class;// int[].class  void.class 返回length也为0
        Field[] fields3 = clazzInteger.getDeclaredFields();
        printFields(fields3);
        
        /*输出
         * DeclaredFields.length=0
         * */
    }

    public static void printFields(Field[] fields) {
        System.out.println("DeclaredFields.length="+fields.length);
        for(Field field:fields){
            StringBuilder sb = new StringBuilder();
            sb.append(Modifier.toString(field.getModifiers())).append(" ")/*修饰符*/
            .append(field.getType().getSimpleName()).append(" ")/*类型*/
            .append(field.getName()).append(" ")/*成员变量名*/
            ;
            System.out.println(sb.toString());
        }
        System.out.println("----------------");
    }
}

 

 

public class StudentTest {

	/**
	 * 反射获取对象的成员变量值
	 */
	public static void main(String[] args) {
		Student student = new Student("lisi",19);

		try {
			/*获取对象的字段值*/
			Field field = student.getClass().getDeclaredField("age");/*public*/
			Field field2 = student.getClass().getDeclaredField("name");/*private*/
			field2.setAccessible(true);
			Field field3 = student.getClass().getDeclaredField("NUM");/*public static final*/
			
			System.out.println(field.get(student));
			System.out.println(field2.get(student));
			System.out.println(field3.get(student));
			
			
			/*public static 直接在类字节码中,也可从类字节码中直接获取而不通过对象*/
			System.out.println(field3.get(null));
			
			
			/*输出
			 * 	19
				lisi
				3
				3*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}