一般写代码时,会写setter和getter方法,但是通过@Data注解时,只需要声明变量就可以。
源码:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
/**
* If you specify a static constructor name, then the generated constructor will be private, and
* instead a static factory method is created that other classes can use to create instances.
* We suggest the name: "of", like so:
*
* <pre>
* public @Data(staticConstructor = "of") class Point { final int x, y; }
* </pre>
*
* Default: No static constructor, instead the normal constructor is public.
*
* @return Name of static 'constructor' method to generate (blank = generate a normal constructor).
*/
String staticConstructor() default "";
}
意思就是:
如果指定静态构造函数名称,则生成的构造函数将是私有的,而是创建静态工厂方法,其他类可以使用该方法创建实例。