为什么要将 getter 和 setter 方法声明为私有?
我看到一段代码,其中 getter 和 setter 方法被声明为私有。我试图弄清楚它背后的逻辑,我真的很难理解为什么你要把它们声明为私有?这与我们试图通过 getter 和 setter 实现的目标完全相反。
I saw a code where getters and setters methods are declared private. I am trying to figure out the logic behind it, and I am really having hard time to understand why would you declare them as private? That's exactly opposite of what we are trying to achieve through getters and setters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以想到几个原因:
如果另一个程序员看到您的代码并想要访问变量,但没有 setter 和 getter,他可能会认为您只是忘记了它们,并自己添加它们。但是,如果您将它们声明为
私有
,则这是一种意图声明,表示我不希望从外部更改或访问这些变量。假设您不需要
public
访问器。但也许您想要计算私有变量更改的次数。使用 setter 比每次访问该变量时增加计数更容易。同样,您不需要
public
访问权限,但在调试过程中,您可能希望在private
成员发生更改的每个位置放置一个断点。因此,您不必在类中的任何地方设置断点,只需在访问器中设置一个断点即可。I can think of several reasons:
If a different programmer sees your code and wants access to a variable, but there are no setters and getters, he might think you just forgot about them, and add them themselves. However, if you declare them as
private
, it's a statement of intent, saying I don't want these variables to be changed or accessed from the outside.Say you don't want
public
accessors. But maybe you want a count of how many times a private variable is changed. It's easier to use a setter rather than incrementing the count every time you access that variable.Again, you don't want
public
access, but during debugging, you might want to put a breakpoint in every place aprivate
member is changed. So instead of setting breakpoints everywhere in the class, you just set one in the accessor.事实上,事实并非如此。声明公共 getter 和 setter 的原因是为了隐藏字段。这样做是为了避免不必要的耦合;即 API 的客户端取决于 API 的实现细节。 (由于多种原因,这种耦合可能会出现问题。例如,对字段类型的依赖、字段发生意外更改的可能性。)
将 getter 和 setter 声明为私有的原因是将对象的抽象状态(即值)的相应部分设为私有。这在很大程度上独立于使用 getter 和 setter 或不隐藏实现类型、防止直接访问等的决定。
虽然使用 getter 和 setter 的情况并不像对于私有国家来说,使用私有 getter 和/或 setter 仍然有明显的好处。例如:
私有 getter/setter 方法提供了添加额外行为或错误检查代码的位置。
它们可以提供记录状态更改或访问字段的位置。
它们可以提供在测试时添加调试代码的位置。 (可能不是最好的调试方法。)
Actually, it is not. The reason for declaring public getters and setters is to hide the fields. This is done to avoid unwanted coupling; i.e. clients of an API depending on the implementation details of the API. (That coupling can be problematic for a number of reasons. For example, dependencies on the types of the fields, the possibility of unexpected changes to the fields.)
The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.
While the case for using getters and setters is not as strong for private state, there are still tangible benefits in using private getters and/or setters. For instance:
The private getter/setter methods provide a place for adding extra behavior or error checking code.
They can provide a place for logging state changes or access to the fields.
They can provide a place for adding your debug code while testing. (Probably not the best way of debugging.)