java的反射

 


 

遍历对象的字段

复制代码
public static void main(String[] args) throws Exception{
        DataSource dataSource = new DataSource();
        reflect(dataSource);
    }
    public static<T> void reflect(T e) throws Exception{
        Class cls = e.getClass();
        Field[] fields = cls.getDeclaredFields();
        for(int i=0; i<fields.length; i++){
            Field f = fields[i];
            f.setAccessible(true);
            System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(e));
        }
    }
复制代码

 

posted @   发疯的man  阅读(187)  评论(0)    收藏  举报

喜欢请打赏

扫描二维码打赏

了解更多

喜欢请打赏

点击右上角即可分享
微信分享提示