lombok和内部类引出的思考
起因
最近看 java编程思想 内部类这篇相关的内容, 感觉之前对内部类的了解比较少
之前使用 lombok , 知道一些注解 , 包括 @Data @AllArgsConstructor @NoArgsConstructor , 还有就是 @Builer 和
@Accessors(chain = true)
之前了解过 @Builder 的原理是 内部类 + builer 模式
实践
新建springboot项目 , 导入 lombok 依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
新建一个 javabean
SysUser
@Getter
@Setter
@ToString
@Builder
public class SysUser {
private Integer id;
private String userName;
}
使用 maven 编译项目
编译后的 class 文件如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.example.demo.web;
public class SysUser {
private Integer id;
private String userName;
SysUser(Integer id, String userName) {
this.id = id;
this.userName = userName;
}
public static SysUser.SysUserBuilder builder() {
return new SysUser.SysUserBuilder();
}
public Integer getId() {
return this.id;
}
public String getUserName() {
return this.userName;
}
public void setId(Integer id) {
this.id = id;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String toString() {
return "SysUser(id=" + this.getId() + ", userName=" + this.getUserName() + ")";
}
public static class SysUserBuilder {
private Integer id;
private String userName;
SysUserBuilder() {
}
public SysUser.SysUserBuilder id(Integer id) {
this.id = id;
return this;
}
public SysUser.SysUserBuilder userName(String userName) {
this.userName = userName;
return this;
}
public SysUser build() {
return new SysUser(this.id, this.userName);
}
public String toString() {
return "SysUser.SysUserBuilder(id=" + this.id + ", userName=" + this.userName + ")";
}
}
}
测试构建一个 SysUser 对象
@Test
void contextLoads() {
SysUser sysUser = SysUser.builder().id(1).userName("测试lombok的@Builer注解").build();
System.out.println(sysUser);
}
控制台打印
SysUser(id=1, userName=测试lombok的@Builer注解)
自己手写
要求:
新建一个 Dept 类 , 属性有 Integer 类型的 id , String 类型的 deptName;
使用 内部类特性 , 以及 builer 模式 , 构建 一个 Dept 类的实例对象
并在控制台打印
第一步
构建 Depte 类
public class Dept {
private Integer id;
private String deptName;
public Dept(){
}
public Dept(Integer id , String deptName){
this.id = id;
this.deptName = deptName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Dept{" +
"id=" + id +
", deptName='" + deptName + '\'' +
'}';
}
}
第二步
使用 内部类 + builer模式
public class Dept {
private Integer id;
private String deptName;
public Dept() {
}
public Dept(Integer id, String deptName) {
this.id = id;
this.deptName = deptName;
}
public static Dept.DeptBuiler builer(){
return new Dept.DeptBuiler();
}
public Integer getId() {
return id;
}
public static class DeptBuiler {
private Integer id;
private String deptName;
DeptBuiler() {
}
public Dept.DeptBuiler id(Integer id) {
this.id = id;
return this;
}
public Dept.DeptBuiler deptName(String deptName) {
this.deptName = deptName;
return this;
}
public Dept build(){
return new Dept(this.id,this.deptName);
}
}
public void setId(Integer id) {
this.id = id;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Dept{" +
"id=" + id +
", deptName='" + deptName + '\'' +
'}';
}
}
第三步
测试
@Test
void contextLoads() {
Dept dept = Dept.builer().id(1).deptName("内部类+builer模式构建javabean").build();
System.out.println(dept);
}
控制台打印
Dept{id=1, deptName=‘内部类+builer模式构建javabean’}