Eclipse JDT:是否有重构使用setter / getter方法替换直接字段访问? [英] Eclipse JDT: Is there a refactoring to replace direct field accesses with setter/getter methods?

查看:215
本文介绍了Eclipse JDT:是否有重构使用setter / getter方法替换直接字段访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有没有人知道如何做这个手动搜索和替换?



谢谢!

解决方案

有一种方法。
专注于属性,右键单击。选择Refactor - >Encapsulate Field。



(编辑解释为什么它可以工作,即使菜单的标题并不暗示)



如果封装字段对话框不存在,将为您创建getter / setter,将更改现场访问权限/ setter方法。该对话框自动检测现有的getter / setter,因此,根据您提到的要求,这将是诀窍。


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!

解决方案

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.

这篇关于Eclipse JDT:是否有重构使用setter / getter方法替换直接字段访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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