java 使用反射获取属性名,和值

Posted 梓★鸿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }

 

以上是关于java 使用反射获取属性名,和值的主要内容,如果未能解决你的问题,请参考以下文章