Does Java bean's setter permit return this?

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

    There is nothing preventing you from providing setter methods which return the target object as a convention in your interface...

    However, you must also use the canonical signature for Java Bean Simple Property setter methods (e.g. void setProp(Type t)) or the bean property will not be recognized as writeable by other software which expects that signature.

提交回复
热议问题