本文介绍了生成在Eclipse中返回自身的设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要我的设置器,以便我可以链接:

I'd like to have my setters so that I can chain them like:

myPojo.setX(x).setY(y);

通常我使用Eclipse生成setter但不幸的是代码模板对于设置器,我只能改变设置者的身体,而不是签名。

Usually I generate setters with Eclipse but unfortunately code template for setters allows me to change only the body of the setter, not the signature.

完成上述什么是最简单的方法?除了搜索和替换+手动编辑? :

What would be the easiest way to complete the above? Besides search-and-replace + manual editing? :)

推荐答案

我可以提供一种补丁,但不需要任何额外的安装。

I can offer a kind of patch that however does not require any additional installations.

转到窗口/首选项/ Java /代码样式/代码模板。编辑setter body模板如下:

Go to Window/preferences/Java/Code Style/Code templates. Edit "setter body" template as following:

${field} = ${param};
return this;

现在,当您运行generate getter and setter时,将创建如下设置器:

Now when you run "generate getters and setters" it will create setter like:

public void setMyField(String myField) {
    this.myField = myField;
    return this;
}

这显然是编译错误,因为方法类型是空隙。但是您可以使用 Ctrl-F ,并将所有public void set替换为 public YourClassName set

This obviously cause compilation error because the method type is void. But you can strike Ctrl-F and replace all 'public void set' by public YourClassName set.

这是一个补丁,但它的工作原理...

It is a patch, but it works...

这篇关于生成在Eclipse中返回自身的设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:52