Eclipse JDT:是否有重构来用 setter/getter 方法替换直接字段访问?
我知道我可以为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个办法。
关注属性,右键单击。选择“重构”-> “封装场”。
(编辑以解释其工作原理,即使菜单的标题并未暗示它)
“封装字段”对话框将为您创建 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.