java 使用反射獲取屬性名,和值


 Class<?> aClass = xxx實體類.getClass();
        //得到屬性
        Field field = null;
        try {
            field = aClass.getDeclaredField("字段名xxxx");
            //打開私有訪問
            field.setAccessible(true);
            //獲取屬性
            String name = field.getName();
            //獲取屬性值
            String subjectType = (String) field.get(xxx實體類);
            System.out.println(subjectType);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

 


免責聲明!

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



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