public class Example { private Long id; // Constructors and other variables and methods deleted for clarity public long getId() { return id; } public void setId(long id) { this.id = this.id == null ? id : this.id; }}
第二种变体(引发异常):
public void setId(long id) { this.id = this.id == null ? id : throw_(); } public int throw_() { throw new RuntimeException("id is already set"); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)