码迷,mamicode.com
首页 > 编程语言 > 详细

java的反射

时间:2019-04-29 17:32:22      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:length   public   name   throw   bsp   ESS   div   print   i++   

 

遍历对象的字段

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

 

java的反射

标签:length   public   name   throw   bsp   ESS   div   print   i++   

原文地址:https://www.cnblogs.com/coder-lzh/p/10791469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!