setter和getter方法是否打破了封装? [英] Do setter and getter methods breaks encapsulation?

查看:143
本文介绍了setter和getter方法是否打破了封装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我们应该避免使用限制器和吸气剂。关于它有各种各样的想法,但根据我使用这些突破封装。为什么?因为它告诉世界一个物体的内部。例如:

It is told that we should avoid setters and getters. There are various school of thoughts about it, but according to me using these breaks encapsulation. Why? because it tells the world about the internals of an object. For example:

class Point {
  private int x;
  private int y;

  void setx(int x) {...}
  int getx() {...}
  ... 
}

该对象应该只暴露为客户提供明确抽象的行为。

The object should just expose the behavior providing the clear abstraction to the client.

class Point {
  private int x;
  private int y;

  int coordinate(int x) {...} // 0, 1, 2, 3
  ... 
}

那么,这些存取器和setter方法是否会破坏封装?

so, is this true that these accessor and setter methods breaks encapsulation?

推荐答案

这里


拥有getter和setter本身并不会破坏封装。
破解封装是什么,每个数据成员(每个字段,在java术语中)都有
的getter和setter。距离将所有数据成员公开只需一步即可获得

Having getters and setters does not in itself break encapsulation. What does break encapsulation is having a getter and a setter for every data member (every field, in java lingo). That is one step away from making all data members public.

封装的重点不是你不应该知道
或从对象外部更改对象的状态,但是
应该有一个合理的策略来执行它。

The point of encapsulation is not that you should not be able to know or to change the object's state from outside the object, but that you should have a reasonable policy for doing it.

考虑类Person的示例。假设一个人有一个名字,
一个社会安全号码和一个年龄。假设我们不允许
的人改变他们的姓名或社会安全号码。但是,
该人的年龄应该每年增加1。在这种情况下,
你会提供一个构造函数来初始化名称和
SSN到给定的值,并将年龄初始化为0.你
也会提供一个方法incrementAge (),这将使
年龄增加1.你还可以为这三个人提供吸气剂。在这种情况下,没有setter是

Consider an example of a class Person. Let's say a person has a name, a social security number, and an age. Let's say that we do not allow people to ever change their names or social security numbers. However, the person's age should be incremented by 1 every year. In this case, you would provide a constructor that would initialize the name and the SSN to the given values, and which would initialize the age to 0. You would also provide a method incrementAge(), which would increase the age by 1. You would also provide getters for all three. No setters are required in this case.

在这个设计中,你允许从类外的
检查对象的状态,并且你允许它从
类之外更改。但是,您不允许任意更改状态。
有一项政策,有效地说明名称和SSN
根本无法更改,并且年龄可以一年增加1

In this design you allow the state of the object to be inspected from outside the class, and you allow it to be changed from outside the class. However, you do not allow the state to be changed arbitrarily. There is a policy, which effectively states that the name and the SSN cannot be changed at all, and that the age can be incremented by 1 year at a time.

现在让我们说一个人也有薪水。人们可以随意换工作
,这意味着他们的薪水也会发生变化。为了模拟这个
情况,除了提供setSalary()方法之外别无他法!
在这种情况下,允许随意更改工资是一个非常合理的
政策。

Now let's say a person also has a salary. And people can change jobs at will, which means their salary will also change. To model this situation we have no other way but to provide a setSalary() method! Allowing the salary to be changed at will is a perfectly reasonable policy in this case.

顺便说一句,在你的例子中,我会给类冰箱
putCheese()和takeCheese()方法,而不是get_cheese()和
set_cheese()。然后你仍然会有封装。

By the way, in your example, I would give the class Fridge the putCheese() and takeCheese() methods, instead of get_cheese() and set_cheese(). Then you would still have encapsulation.

你也可以参考:为什么getter和setter方法是邪恶的

这篇关于setter和getter方法是否打破了封装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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