Field類使用以及getDeclaredFields方法


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

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM