Eclipse生成getter和setter并自动应用它们 [英] Eclipse generate getters and setters and automatically apply them

查看:178
本文介绍了Eclipse生成getter和setter并自动应用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java代码中,我直接访问了一些成员变量。现在我想重构和使用吸气剂和设置器。如何使Eclipse自动替换所有的直接赋值与每个访问getter?



右键单击 - >源 - >生成Getter和Setter只是创建功能,但不适用于其余的代码。

解决方案

使用 Refactor 菜单。它有一个项目封装字段,它生成getter和setter,就像Source - > Generate Getters and Setters一样,但是它替换了所有字段的用法,并使这些字段变为私有。



(作为助记符:要更改现有代码的结构(即重构它),请使用 Refactor 菜单生成新代码尚未使用,请使用 Source 菜单,也就是说,如果您使用一些getter和setter创建一个新类,则可以使用来源菜单通过仅对字段进行编码,然后自动生成getter和setter,但是如果您希望现有的代码使用getter和setter而不是直接的现场访问,这是代码 Refactor ing。)


In my Java code I directly accessed some member variables. Now I want to refactor and use getter and setters. How can I make Eclipse automatically replace all direct assignments with setters and each access with a getter?

Right click -> Source -> Generate Getters and Setters just creates the functions but does not apply them in the rest of the code.

解决方案

Use the Refactor menu. It has an item "encapsulate fields" which generates getters and setters just as "Source -> Generate Getters and Setters" does, but in addition it replaces all usages of the fields and makes the fields private.

(As a mnemonic: To change the structure of existing code (i.e. to refactor it), use the Refactor menu. To generate new code that is not used yet, use the Source menu. That is, if you create a new class with some getters and setters, you can use the help of the Source menu by coding the fields only and auto-generate the getters and setters afterwards. However if you want existing code to use getters and setters instead of direct field access, this is a classic case of code Refactoring.)

这篇关于Eclipse生成getter和setter并自动应用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆