如何在自动生成getter和setter时强制Eclipse使用this关键字?(How to force Eclipse to use the this keyword when auto-generating getters and setters?)

在Eclipse中,有没有办法在自动生成getter和setter时强制使用this关键字引用类成员?

例如,我想要这个由Eclipse自动生成的getter,

public int getId() { return id; }

回答:

(答案由维塔利提供,但为了清楚:)

在顶部菜单中,单击“ 窗口”>“首选项” 。 然后,在“ 首选项”窗口中,单击“ Java”>“代码样式” 您将看到一个复选框,其中包含Qualify所有生成的字段访问次数为'this'。 勾选此复选框。

奇迹般有效。

(注意'this.'必须是'this'. 。)

In Eclipse, is there a way to force the use of the this keyword with referring to class members when auto-generating getters and setters?

For example, I want this getter, auto-generated by Eclipse,

public int getId() { return id; }

ANSWER:

(The answer is provided by Vitaly, but to make things clear:)

In the top menu, click Window > Preferences. Then, in the Preferences window, click Java > Code Style You'll see a checkbox with Qualify all generated field accesses with 'this.' Tick this checkbox.

Works like a charm.

(Notice that 'this.' must be 'this'..)

最满意答案

Source -> Generate Getters and Setters

在Code Templates有一个链接,点击它。

选择 - 您将更改项目设置或工作区设置。

在Configure generated code and comments ,展开Code并选择Getter Body ,在模式中添加this. 。

而已。

注意:您可以从项目属性或工作区首选项中打开Code Templates 。

Open Source -> Generate Getters and Setters

There is a link at the botton to Code Templates, click it.

Choose - will you change project settings or workspace settings.

In Configure generated code and comments, expand Code and select Getter Body, in pattern add this..

That's it.

Note: You can open Code Templates from project properties or from workspace preferences.

更多推荐