@Getter和@Setter

99 篇文章 0 订阅
59 篇文章 2 订阅

@Getter和@Setter 出现的目的是
public int getFoo() {return foo;} 不需要在写get 和 set 方法。

您可以使用@Getter或@Setter来注释任何字段,以使lombok自动生成默认的getter / setter。

lombok生成的getter / setter方法默认作用域将是public
除非你明确指定一个AccessLevel

如下面的例子所示。作用域级别PUBLIC,PROTECTED,PACKAGE,和PRIVATE。

你也可以在class 上面放置@Getter和/或@Setter注释。在这种情况下,就好像您使用注释注释该类中的所有非静态字段。

使用了Lombok 代码

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class GetterSetterExample {
  /**
   * Age of the person. Water is wet.
   * 
   * @param age New value for this person's age. Sky is blue.
   * @return The current value of this person's age. Circles are round.
   */
  @Getter @Setter private int age = 10;

  /**
   * Name of the person.
   * -- SETTER --
   * Changes the name of this person.
   * 
   * @param name The new value.
   */
  @Setter(AccessLevel.PROTECTED) private String name;

  @Override public String toString() {
   return String.format("%s (age: %d)", name, age);
  }
}

默认的代码是(未使用lombok)

public class GetterSetterExample {
  /**
   * Age of the person. Water is wet.
   */
  private int age = 10;

  /**
   * Name of the person.
   */
  private String name;

  @Override public String toString() {
    return String.format("%s (age: %d)", name, age);
  }

  /**
   * Age of the person. Water is wet.
   *
   * @return The current value of this person's age. Circles are round.
   */
 public int getAge() {
    return age;
  }

  /**
   * Age of the person. Water is wet.
   *
   * @param age New value for this person's age. Sky is blue.
   */
  public void setAge(int age) {
    this.age = age;
  }

  /**
   * Changes the name of this person.
   *
   * @param name The new value.
   */
  protected void setName(String name) {
    this.name = name;
  }
}

要在生成的方法上注释,可以使用onMethod=@({@AnnotationsHere}); 将注释放在生成的setter方法的唯一参数上,可以使用onParam=@({@AnnotationsHere})。小心!这是一个实验功能。

getter / setter参数

lombok.AccessLevel value() default lombok.AccessLevel.PUBLIC;

lombok.Getter.AnyAnnotation[] onMethod() default {};

boolean lazy() default false;

getter / setter使用方法

package me.wonwoo;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;



public class GetSetObject {

//  @Setter(onParam = @__({@NotNull}), onMethod = @__({@NotNull}))
  @Setter(onMethod = @__({@NotNull}))
//  @Getter(value = AccessLevel.PUBLIC, onMethod = @__({@NonNull, @Id}))
  private Long id;
//  @Getter
  @Getter(value = AccessLevel.PUBLIC, lazy = true)
  private final String name = expensive();

  private String expensive() {
    return "wonwoo";
  }
}

class GetSetObjectOnParam {
  private Long id;

  public void setId(@NotNull Long id) {
    this.id = id;
  }
}

class GetSetObjectOnMethod {
  private Long id;

  @Id
  @Column(name = "seq")
  Long getId() {
    return id;
  }
}


作者:二月长河
链接:https://www.jianshu.com/p/93353398e964
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值