Eclipse 可以生成方法链接 setter
我想生成方法链接设置器(返回正在设置的对象的设置器),如下所示:
public MyObject setField (Object value) {
this.field = value;
return this;
}
这使得更容易进行单行实例化,我发现它更易于阅读:
myMethod (new MyObject ().setField (someValue).setOtherField (someOtherValue));
Can Eclipse's templates be generated to do this? 我已更改内容以包含 return this;
但签名未更改。
I'd like to generate method-chaining setters (setters that return the object being set), like so:
public MyObject setField (Object value) {
this.field = value;
return this;
}
This makes it easier to do one-liner instantiations, which I find easier to read:
myMethod (new MyObject ().setField (someValue).setOtherField (someOtherValue));
Can Eclipse's templates be modified to do this? I've changed the content to include return this;
but the signature is not changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确认 Eclipse(最高 3.5RC1)不支持“方法链接”setter 生成。
它只允许注释和正文自定义,不允许对 setter 进行 API 修改(这意味着生成的 setter 仍返回“
void
”)。可能插件 Builder Pattern 可以在这里提供帮助......(不是虽然经过测试)
经典方式(不是“傻瓜”,因为它总是生成一个“
void
”作为setter的返回类型):(来源: eclipse.org)
与。 新方式(构建器模式,可能用作Eclipse 插件)
替代文本 http://www.javadesign。信息/媒体/博客/JDesign/DesignConcepts/DesignPatterns/GOF/Creational-BuilderPatternStructure.jpeg
I confirm eclipse (up to 3.5RC1) does not support "method chaining" setter generation.
It only allows for comment and body customization, not API modification of a setter (meaning a generated setter still return '
void
').May be the plugin Builder Pattern can help here... (not tested though)
Classic way (not "goof" since it will always generate a "
void
" as return type for setter):(source: eclipse.org)
Vs. new way (Builder Pattern, potentially used as an Eclipse plugin)
alt text http://www.javadesign.info/media/blogs/JDesign/DesignConcepts/DesignPatterns/GOF/Creational-BuilderPatternStructure.jpeg
我自己不要使用 Eclipse,但如果找不到某个功能,则必须更改标准模板之一。
顺便说一句,它被称为方法链(这可能有助于 Google 搜索一两次)。
Don't use eclipse myself, but you'll have to change one of the standard templates if you can't find a feature.
It's called method chaining by the way (which might help with a Google search or two).