我已經開始使用從jQuery中採用的一種技術,在我的setter中稱爲鏈接。例如:鏈接setter:有沒有缺點?
class Foo {
private int bar;
private int baz;
public int getBar() { return this.bar; }
public int getBaz() { return this.baz; }
public Foo setBar(int bar) {
this.bar = bar;
return this;
}
public Foo setBaz(int baz) {
this.baz = baz;
return this;
}
}
我更喜歡這種方式,因爲它可以很容易地設置多個屬性。
有沒有缺點?我曾經問過幾個Java程序員,但他們只能說這是「不好」,但他們無法給我一個正確的解釋。
可能的重複[是不好的做法,使setter返回「this」?](http://stackoverflow.com/questions/1345001/is-it-bad-practice-to-make-a-setter-返回此) – Femaref 2011-02-01 23:43:40