我注意到在我生成的实体中 - (这里称为模式见下文),有一些方法添加了返回实例,例如:
public Pattern name(String name) {
this.name = name;
return this;
}
...
public Pattern numCandles(Integer numCandles) {
this.numCandles = numCandles;
return this;
}
以上这些功能有哪些?
实体
@Entity
..
public class Pattern implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(max = 20)
@Column(name = "name", length = 20, nullable = false, unique = true)
private String name;
@NotNull
@Column(name = "num_candles", nullable = false)
private Integer numCandles;
@Column(name = "jhi_desc")
private String desc;
...
}```
返回实例允许您链接对象的方法,如下所示:
Pattern p = new Pattern().name('x').numCandles(2);
这些被称为fluent methods
,主要用于提高源代码的可读性