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)])

  • 相关阅读:
    [HDU2136] Largest prime factor(素数筛)
    [luoguP1082] 同余方程(扩展欧几里得)
    基本数论算法
    [luoguP2444] [POI2000]病毒(AC自动机 + dfs)
    [luoguP2564] [SCOI2009]生日礼物(队列)
    在EditText插入表情,并发送表情
    程序员自我提高的几点建议
    CSS3悬停特效合集Hover.css
    带动画效果的jQuery手风琴
    android程序的真正入口
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/12072250.html
Copyright © 2011-2022 走看看