我可以在一个类别的Lombok中同时使用@data和@builder注释吗?

问题描述 投票:-1回答:1

我要使用的原因是因为我想以这种格式生成设置器

针对班级

public class Person {
    private String firstName;
    private String lastName;
}


 public Person setFirstName(String firstName) {
        this.firstName = firstName;
        return this;
    }

    public Person setLastName(String lastName) {
        this.lastName = lastName;
        return this;
    }

而不是@Data生成的二传手

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

或者还有其他方法吗?请让我知道。

java lombok
1个回答
0
投票

为此,您应该使用@Accessors(chain = true)。例如:

@Accessors(chain = true)
@Setter
public class Person {
    private String firstName;
    private String lastName;
}

香草Java表示形式:

public class Person {
    private String firstName;
    private String lastName;

    public Person setFirstName(String firstName) {
        this.firstName = firstName;
        return this;
    }

    public Person setLastName(String lastName) {
        this.lastName = lastName;
        return this;
    }
}