zoukankan      html  css  js  c++  java
  • 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();
    		}
    	}
    
    }
    
  • 相关阅读:
    luogu P1330 封锁阳光大学 x
    luoguP3353 在你窗外闪耀的星星
    luogu小金明qwq x
    [HDOJ5093] Battle ships(最大匹配)
    [HDOJ5092] Seam Carving(DP,记录路径)
    [UVA1449] Dominating Patterns(AC自动机,STL,计数,神坑)
    [POJ3057]Evacuation(二分图匹配,BFS,二分,好题)
    [POJ3041]Asteroids(二分图,最大匹配)
    [POJ2195]Going Home(带权最大匹配,KM,最小费用流)
    [codeVS1917] 深海机器人问题(费用流,拆边)
  • 原文地址:https://www.cnblogs.com/beenupper/p/2618609.html
Copyright © 2011-2022 走看看