Lombok(1.14.8) - @NoArgsConstructor, @RequiredArgsConstructor & @AllArgsConstructor

@NoArgsConstructor

@NoArgsConstructor,提供一个无参的构造方法。

package com.huey.hello.bean;

import java.util.Date;

import lombok.Data;
import lombok.NoArgsConstructor; @NoArgsConstructor
@Data
public class Student { private int studNo;
private String studName;
private Date birthday; // 提供了一个含参构造方法,就不会再默认提供无参构造方法
public Student(int studNo, String studName, Date birthday) {
super();
this.studNo = studNo;
this.studName = studName;
this.birthday = birthday;
} public static void main(String[] args) {
Student stud = new Student();
stud.setStudNo(10001);
stud.setStudName("huey");
stud.setBirthday(new Date());
System.out.println(stud); // [OUTPUT]: Student(studNo=10001, studName=huey, birthday=Sat Aug 01 00:03:43 CST 2015)
}
}

@RequiredArgsConstructor

@RequiredArgsConstructor,为所有 final 和 @NonNull 修饰的字段生成一个构造方法。

package com.huey.lombok;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter; @RequiredArgsConstructor
public class RequiredArgsConstructorExample { @Getter
private final String finalField;
@NonNull
@Getter @Setter
private String nonNullField;
@Getter @Setter
private String commonField; public static void main(String[] args) {
RequiredArgsConstructorExample foo = new RequiredArgsConstructorExample("FINAL FIELD", "NON NULL FIELD");
System.out.println(foo.getFinalField()); // [OUTPUT]: FINAL FIELD
System.out.println(foo.getNonNullField()); // [OUTPUT]: NON NULL FIELD
} }

@AllArgsConstructor

@AllArgsConstructor,提供一个全参的构造方法。

package com.huey.hello.bean;

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Data; @AllArgsConstructor
@Data
public class Student { private int studNo;
private String studName;
private Date birthday; public static void main(String[] args) {
Student stud = new Student(10001, "huey", new Date());
System.out.println(stud); // [OUTPUT]: Student(studNo=10001, studName=huey, birthday=Sat Aug 01 00:03:43 CST 2015)
}
}
上一篇:HDU_4826


下一篇:java-多人连接