Eclipse JDT:是否有重构来用 setter/getter 方法替换直接字段访问?

发布于 2024-09-27 20:37:23 字数 119 浏览 1 评论 0原文

我知道我可以为 Eclipse 源菜单中的字段生成 setter 和 getter,但我非常惊讶它没有提供用对新创建的方法的调用来替换直接字段访问器。

有谁知道如何进行手动搜索和替换?

谢谢!

I know I can generate setters and getters for fields in the Eclipse source menu, but I'm very surprised it doesn't offer to replace the direct field accessors with calls to the newly created methods.

Does anyone know how to do this short of manual search and replace?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬心 2024-10-04 20:37:23

有一个办法。
关注属性,右键单击。选择“重构”-> “封装场”。

(编辑以解释其工作原理,即使菜单的标题并未暗示它)

“封装字段”对话框将为您创建 getter/setter(如果它们不存在), 将更改字段访问以通过 getter/setter。该对话框会自动检测现有的 getter/setter,因此,对于您提到的要求,这可以解决问题。

There is a way.
Focus on the attribute, right click. Select "Refactor" -> "Encapsulate Field".

(Edited to explain why it works, even though the menu's title doesn't imply it)

The "Encapsulate Field" dialog-box will create getters/setters for you if they don't exist, and will change field-access to go through the getters/setters. The dialog automatically detects existing getters/setters so, for the requirements that you had mentioned, that would do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文