Does Java bean's setter permit return this?

后端 未结 10 545
一向
一向 2020-12-06 10:02

can I define setter method to return this rather than void?

Like:

ClassA setItem1() {
      return this;
}

ClassA setItem2() {
      return this;
}
         


        
10条回答
  •  太阳男子
    2020-12-06 10:44

    Yes. This is a somewhat common technique called Method Chaining, and can be used to create a "fluent interface".

    See: http://en.wikipedia.org/wiki/Method_chaining, http://en.wikipedia.org/wiki/Fluent_interface

提交回复
热议问题