美文网首页
遍历对象属性Demo

遍历对象属性Demo

作者: SamHxm | 来源:发表于2019-06-11 23:23 被阅读0次
    copy /y/e/s E:\workspace3\learn\api\out\production\classes E:\tt
    

    测试代码

    package com.sam.learn;
    
    import com.sam.learn.api.dto.BookDTO;
    import com.sam.learn.api.dto.UserDTO;
    
    import java.lang.reflect.Field;
    import java.util.*;
    public class AppStart {
        public static void main(String[] args) throws Exception {
            UserDTO user = new UserDTO();
            user.setName("Scott");
            user.setAge(20);
            user.setPhone("13600000000");
    
            List list = new ArrayList();
            list.add(new BookDTO("Java", 1));
            list.add(new BookDTO("C++", 2));
            user.setBooks(list);
    
            Map map = new HashMap<>();
            map.put(new Date(), new BookDTO("Java30", 30));
            map.put(new Date(), new BookDTO("C++40", 40));
            user.setMap(map);
    
            BookDTO[] bookDTOArray = new BookDTO[]{new BookDTO("Java300", 300), new BookDTO("C++400", 400)};
            user.setBookDTOArray(bookDTOArray);
    
            reflect(user, null, "-1");
    
    
        }
    
        public static void reflect(Object e, String parentName, String index) throws Exception {
            Class cls = e.getClass();
            Field[] fields = cls.getDeclaredFields();
            for(Field f : fields){
                f.setAccessible(true);
                if(Collection.class.isAssignableFrom(f.getType())){
                    Iterator iterator = ((Collection)f.get(e)).iterator();
                    int j = 0;
                    while(iterator.hasNext()){
                        reflect(iterator.next(), f.getName(), "[" + String.valueOf(j) + "]");
                        j++;
                    }
                    continue;
                } else if(Map.class.isAssignableFrom(f.getType())){
                    Map map = (Map)f.get(e);
                    Iterator iterator = map.entrySet().iterator();
                    int j = 0;
                    while(iterator.hasNext()){
                        Map.Entry entry = (Map.Entry) iterator.next();
                        reflect(entry.getValue(), f.getName(), "[" + String.valueOf(j) + "](key:" + entry.getKey() + ")");
                        j++;
                    }
                    continue;
                } else if(f.getType().isArray()){
                    Object[] objectArray = (Object[])f.get(e);
                    int j = 0;
                    for(Object obj : objectArray){
                        reflect(obj, f.getName(), "[" + String.valueOf(j) + "]");
                        j++;
                    }
                    continue;
                }
    
                if(parentName != null && !index.equals("-1")){
                    System.out.println(parentName + "." + f.getName() + index + " = " + f.get(e));
                } else {
                    System.out.println(f.getName() + " = " + f.get(e));
                }
    
            }
        }
    }
    
    package com.sam.learn.api.dto;
    
    public class BookDTO {
    
        private String name;
        private Integer id;
    
        public BookDTO(String name, Integer id){
            this.name = name;
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    }
    
    
    package com.sam.learn.api.dto;
    
    import java.util.List;
    import java.util.Map;
    
    public class UserDTO {
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public List<BookDTO> getBooks() {
            return books;
        }
    
        public void setBooks(List<BookDTO> books) {
            this.books = books;
        }
    
        public Map getMap() {
            return map;
        }
    
        public void setMap(Map map) {
            this.map = map;
        }
    
        public BookDTO[] getBookDTOArray() {
            return bookDTOArray;
        }
    
        public void setBookDTOArray(BookDTO[] bookDTOArray) {
            this.bookDTOArray = bookDTOArray;
        }
    
        private String name;
        private Integer age;
        private String phone;
        private List<BookDTO> books;
        private Map map;
        private BookDTO[] bookDTOArray;
    }
    
    

    相关文章

      网友评论

          本文标题:遍历对象属性Demo

          本文链接:https://www.haomeiwen.com/subject/xvrhfctx.html