Does Java bean's setter permit return this?

后端 未结 10 587
一向
一向 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:49

    Absolutely nothing to stop you doing that but why. If you want to do this create a constructor that takes the args. Bare in mind some software that uses beans would not be expecting return values and may have some unexpected results

    If you just want to simplify initialisation, (maybe to set up tests) you could use some groovy code.

提交回复
热议问题