【问题标题】:Chaining setters: is there a drawback?链接设置器:有缺点吗?
【发布时间】:2011-02-01 23:39:25
【问题描述】:

我已经开始使用一种从 jQuery 中采用的技术,在我的 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 程序员,但他们只能说它“不好”,但他们无法给我一个正确的解释。

【问题讨论】:

标签: architecture software-design


【解决方案1】:

我认为您遇到了一个缺点:在某些编程语言的文化中,这是不寻常的和非传统的。有些文化对不寻常和非常规的反应比其他文化更好。我同意它有优点,但 Java 设置器通常不是这样编码的。

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2010-12-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多