@Builder和构造函数同时使用

Lombok @

新建一个使用@Builder注解和构造函数的类

@Data
@Builder
public class TestBuilder {

    private String name;
    private String desc;

    public TestBuilder() {

    }

    public TestBuilder(String desc, String name) {
        this.name = name;
        this.desc = desc;
    }
}

测试结果输出

    public static void main(String[] args) {
        TestBuilder builder = TestBuilder.builder()
        .desc("desc")
        .name("name").build();
        System.out.println(builder.toString());
    }
    // 输出结果
    // TestBuilder(name=desc, desc=name)

错误原因

        // 参数的顺序错了 
    public TestBuilder(String desc, String name) {
        this.name = name;
        this.desc = desc;
    }

这里应该修改成:

    public TestBuilder(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

我的个人博客,有空来坐坐

你可能感兴趣的:(@Builder和构造函数同时使用)