zoukankan      html  css  js  c++  java
  • java内部类

    1、内部静态类

    import cn.hutool.core.collection.CollUtil;
    import com.alibaba.fastjson.JSONObject;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    
    import java.util.List;
    
    /**
     * @author www.gomepay.com
     * @date 2019/12/20
     */
    @Data
    @AllArgsConstructor
    public class Dept {
        String deptName;
        List<User> list;
        @Data
        @AllArgsConstructor
        public static class User {
            String userName;
        }
    
        public static void main(String[] args) {
            List<User> list = CollUtil.newArrayList(new User("y1"),new User("y2"));
            Dept dept = new Dept("研发部",list);
            String jsonString = JSONObject.toJSONString(dept);
            Dept dept2 = JSONObject.parseObject(jsonString,Dept.class);
            System.out.println(dept2);
        }
    }

    输出:Dept(deptName=研发部, list=[Dept.User(userName=y1), Dept.User(userName=y2)])

    2、内部普通类

    import cn.hutool.core.collection.CollUtil;
    import com.alibaba.fastjson.JSONObject;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.List;
    
    /**
     * @author www.gomepay.com
     * @date 2019/12/20
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Dept2 {
        String deptName;
        List<User> list;
        @Data
        @AllArgsConstructor
        public class User {
            String userName;
        }
    
        public static void main(String[] args) {
            List<User> list = CollUtil.newArrayList(new Dept2().new User("y1"),new Dept2().new User("y2"));
            Dept2 dept = new Dept2("研发部",list);
            String jsonString = JSONObject.toJSONString(dept);
            Dept2 dept2 = JSONObject.parseObject(jsonString, Dept2.class);
            System.out.println(dept2);
        }
    }

    输出:Dept2(deptName=研发部, list=[Dept2.User(userName=y1), Dept2.User(userName=y2)])

  • 相关阅读:
    前端面试题
    js collection
    javascript变量声明提升(hoisting)
    css3动画
    神奇的meta
    wap站bug小结
    前端collection
    js拾遗
    prototype之初印象
    自定义scrollBottom的值
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/12072250.html
Copyright © 2011-2022 走看看