我是否应该指定setter?

发布于 2024-09-13 04:15:07 字数 475 浏览 2 评论 0原文

这是一个简单的 Address 类的初始规范。这是一种简化,因为它忽略了复杂性,例如同一建筑物中的公寓可能具有相同的“编号”,
例如29a、29b。

 class Address
    {
    private:
    int number;
    string name;
    string postcode;
    public:
    //getters implemented but ommited 
    };

如果 Address 被假定为实用程序类(其他开发人员将来可能在其他项目中使用):

//QUESTION
1.对于上面规范中给出的 Address 的每个属性,请说明是否适合为 Address 类配备相应实例变量的 setter 方法。对每种情况给出简短的理由。

伙计们,这是我作业中的一个问题,所以请不要质疑类 Address 的设计方式。

Here is an initial specification for a simple Address class. This is a simplification as it ignores complications such as apartments in the same building potentially having the same ‘number’,
e.g. 29a, 29b.

 class Address
    {
    private:
    int number;
    string name;
    string postcode;
    public:
    //getters implemented but ommited 
    };

If Address is suppose to served as an utility class (possible future use in other projects by other developers):

//QUESTION
1. For each attribute of Address given in the specification above, state whether it would be appropriate to equip the Address class with a setter method for the corresponding instance variable. Give a brief justification in each case.

Guys this is a question from my assignment so please do not question the way class Address is designed.

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

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

发布评论

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

评论(4

究竟谁懂我的在乎 2024-09-20 04:15:07

取决于地址的来源。比如说,如果您从数据库中读取它,那么我不会实现设置器,因为您不希望人们在没有正确权限的情况下更改您的数据库值。但是,如果您从用户那里读取这些数据,那么您将必须考虑用户进行拼写错误和调整或意识到他们输入了旧地址或任何此类情况的事实,并且您必须提供更改。

Depends on the source of Address. If, say, you read it from a database, then I wouldn't implement setters as you don't want people changing your database values without the correct permissions. If, however, you read this data in from the user, then you will have to account for the fact that users make typos and adjustments or realize they entered their old address or any of that, and you must provide for the changes.

丑丑阿 2024-09-20 04:15:07

对我来说,这应该是一个不可变的类,所有字段都在构造时设置,并且每个字段都有 getter。

To me that should be an immutable class, with all fields set at construction time, and getters for each.

帅气尐潴 2024-09-20 04:15:07

我认为地址应该是不可变的,因为地址本身不能改变。因此,如果一个人更改了地址,则应附加一个新对象。

不确定这里的“名称”是什么意思,如果它是一条错误命名的街道或人名。

I think an address should be immutable, as an address itself cannot change. So if a person changes his address, a new object should be attached.

Not sure what "name" means here, if it is a mis-named street or the name of the person.

柠北森屋 2024-09-20 04:15:07

在我的 pov 中应该是一个不可变的类,所有字段在构造时设置,每个字段的吸气剂。

如果您确实需要在构造后更改地址,请考虑为完整的字段集设置一个设置器,以避免修改单个字段而导致对象数据不一致。
完全取决于对象的用途。

Should be an immutable class in my pov, with all fields set at construction time, getter for each field.

If you really need to alter the address after construction, think about a setter for the complete set of fields to avoid modification of a single field making the objects data inconsistent.
Depends completely on the use of the object.

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