不同的构造函数(LOMBOK)

huangapple go评论78阅读模式
英文:

Different constructors (LOMBOK)

问题

我有一个类看起来像这样:

@EqualsAndHashCode
@RequiredArgsConstructor
public class StatusUpdate {

    @Getter
    @Setter
    private Long id;

    @Getter
    @Setter
    @NonNull
    private String text;

    @Getter
    @Setter
    @NonNull
    private Date added;    
}

我想使用 Lombok 创建以下两个构造函数:

public StatusUpdate(String text) {
     this.text = text;
}

public StatusUpdate(String text, Date added) {
     this.text = text;
     this.added = added;
}

我尝试过使用以下三个注解:
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor

但是我无法通过这些来实现,我只有一个带有两个参数的构造函数,所以我需要另一个仅带一个参数的构造函数。我阅读了这个主题:@SomeArgsConstructor,这就是我需要的,但是由于这个不存在,我想我应该手动创建一个所需的单参数构造函数,而其他构造函数将使用 Lombok 来处理。还有没有更好/更优雅的方法只使用 Lombok 来实现呢?

谢谢!

英文:

I have a class that looks like this:

@EqualsAndHashCode
@RequiredArgsConstructor
public class StatusUpdate {

	@Getter
	@Setter
	private Long id;
	
	@Getter
	@Setter
	@NonNull
	private String text;
	
	@Getter
	@Setter
	@NonNull
	private Date added;	
}

And I want to create these two constructors using Lombok:

public StatusUpdate(String text) {
     this.text = text;
}

public StatusUpdate(String text, Date added) {
     this.text = text;
     this.added = added;
}

I tried using all three annotations:
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor

But I couldn't do that with these, I only have one constructor that has two parameters, so I need one more constructor with one parameter only. I read this topic: @SomeArgsConstructor and this is what I need but since this does not exists I guess I should create manually one arg constructor that I need and other constructors I'll handle with Lombok, or is there any better / more elegant way to do it using Lombok only?

Thanks!

答案1

得分: 2

是的,你应该自己添加它们。几年前已经有一个讨论关于添加@SomeArgsConstructor注解的,但由于Lombok团队从未添加过该注解,我认为他们不太可能会添加。

或者,如评论中所述,可以使用带有@Builder注解的构建器模式。然后你可以编写类似这样的代码:StatusUpdate.builder().text("text").date(new Date()).build();


顺便说一下,如果你将所有字段都用@Getter@Setter进行注解,并在类级别上使用@EqualsAndHashCode@RequiredArgsConstructor,我认为@Data注解可能很适合这个类。

英文:

Yeah, you should just add them yourself. Years ago there was already a discussion to add the @SomeArgsConstructor annotation, but since the Lombok team never did add that annotation, I think it is unlikely they will ever do it.

Or, as stated in the comments, use the builder pattern with the @Builder annotation. Then you could write something like: StatusUpdate.builder().text("text").date(new Date()).build();.


Btw, if you do annotate all your fields with @Getter, @Setter and use the @EqualsAndHashCode and @RequiredArgsConstructor on class level, I think the @Data annotation could be a good fit for this class.

答案2

得分: 0

使用 @Builder 注解在你的实体类中,并手动构建你的对象。

User user = User.builder()
                .username(signupDto.getUsername())
                .email(signupDto.getEmail())
                .password(encoder.encode(signupDto.getPassword()))
                .roles(roles)
                .build();
英文:

use @Builder annotation with your entity class and build your object manually.

 User user = User.builder()
                    .username(signupDto.getUsername())
                    .email(signupDto.getEmail())
                    .password(encoder.encode(signupDto.getPassword()))
                    .roles(roles)
                    .build();

huangapple
  • 本文由 发表于 2020年8月20日 18:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63502702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定