lombok 基础注解之 @AllArgsConstructor

最全的 lombok 注解详情(随着版本不定时更新)

一、注解介绍

作用于类,生成一个参数为所有实例变量的构造方法

二、属性介绍

  • staticName:使生成的构造方法是私有的,默认值 “”
    并且生成一个参数为所有实例变量,返回类型为当前对象的静态方法,方法名为 staticName 值
  • onConstructor:列出的所有注解都放在生成的构造函数上
    JDK 7 之前的写法 onConstructor = @__({@AnnotationsGoHere}),JDK 8 之后的写法 onConstructor_ = {@AnnotationsGohere}
  • access:设置构造函数的访问修饰符,如果设置了 staticName,那么将设置静态方法的访问修饰符
    默认:PUBLIC,共有 PUBLIC、MODULE、PROTECTED、PACKAGE、PRIVATE、NONE
    其中 MODULE 是 Java 9 的新特性,NONE 表示不生成构造函数也不生成静态方法,即停用注解功能

三、实战演练

@AllArgsConstructor(staticName = "newInstancec", access = AccessLevel.PROTECTED, onConstructor_ = {
    
    @Deprecated})
public class 鞠婧祎 {
    
    
	private String name;
	
	public static int age;
	
	@NonNull
	private String address;
}
编译后
public class 鞠婧祎 {
    
    
	private String name;
	
	public static int age;
	
	@NonNull // 字段不允许为空
	private String address;
	
	@Deprecated
	private 鞠婧祎(String name, @NonNull String address) {
    
    
		if (address == null)
			throw new NullPointerException("address is marked non-null but is null");
		this.name = name;
		this.address = address;
	}
	
	protected static 鞠婧祎 newInstancec(String name, @NonNull String address) {
    
    
		return new 鞠婧祎(name, address);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39249094/article/details/120860162