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

 

posted @ 2020-05-31 23:31  随★风  阅读(12645)  评论(0编辑  收藏  举报