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

 

posted @ 2019-12-20 11:36  遥远2  阅读(256)  评论(0编辑  收藏  举报