如何避免getter和setter [英] How to avoid getters and setters

查看:116
本文介绍了如何避免getter和setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多地方都读到吸血鬼和塞特犬是邪恶的".我知道为什么会这样.但是我不知道如何完全避免它们.说项目是具有有关项目名称,数量,价格等信息的类. ItemList是一个类,它具有项目列表.要找到总计:

I have read in many places that "getters and setters are evil". And I understood why so. But I don't know how to avoid them completely. Say Item is a class that has information about item name, qty, price etc... and ItemList is a class, which has a list of Items. To find the grand total:


int grandTotal()
{
int total = 0;

for (Item item: itemList)
       total += item.getPrice();

return total;
}

在上述情况下,如何避免getPrice()? Item类提供getName,setName等.

In the above case, how does one avoid getPrice()? The Item class provides getName, setName, etc....

如何避免它们?

推荐答案

什么时候应该使用getter和setter?

字母和设置者非常适合配置或确定类的配置,或从模型中检索数据

When should you use getters and setters?

Getters and setters are great for configuring or determining the configuration of a class, or retrieving data from a model

获取一件商品的价格完全是对吸气剂的合理使用.那就是需要可用的数据,并且可能涉及特殊的注意事项,以通过向设置器添加验证或清除来保护数据.

Getting the price of an item is an entirely reasonable use of a getter. That is data that needs to be available and may involve special considerations to protect the data by adding validation or sanitization to the setter.

您还可以提供不带setter的getter.他们不必成对出现.

You can also provide getters without setters. They do not have to come in pairs.

有时对象依赖于永远不会公开的内部属性.例如,迭代器和内部集合.暴露内部集合可能会产生严重的负面和意想不到的后果.

Sometimes objects rely on internal properties that will never be exposed. For example, Iterators and internal collections. Exposing the internal collection could have dramatically negative and unexpected consequences.

例如,假设您正在通过某些HttpURLConnection进行通信.公开HttpURLConnection的setter意味着,如果在等待接收数据时更改了连接,则最终可能会遇到非常奇怪的状态.这种连接应该在实例化时创建,或者完全在内部进行管理.

Also, for example, let's say you are communicating via some HttpURLConnection. Exposing the setter for your HttpURLConnection means that you could end up with a very odd state should the connection be changed while waiting to receive data. This connection is something that should be created on instantiation or entirely managed internally.

如果您拥有用于所有意图和目的的公共数据,但需要进行管理:请使用getter和setter.

If you have data that is for all intents and purposes public, but needs to be managed: use getters and setters.

如果您有需要检索的数据,但在任何情况下都不得更改:请使用getter而不是setter.

If you have data that needs to be retrieved but under no circumstances should ever be changed: use a getter but not a setter.

如果您需要为内部目的而设置数据,并且永远不要公开暴露它们(并且不能在实例化时进行设置):请使用setter,而不要使用getter(setter可以防止第二次调用影响内部属性)

If you have data that needs to be set for internal purposes and should never be publicly exposed (and cannot be set at instantiation): use a setter but not a getter (setter presumably prevents a second call affecting the internal property)

如果您的内容完全是内部的,并且没有其他类需要访问或直接更改它,则不要使用.

If you have something that is entirely internal and no other class needs to access it or change it directly, then use neither.

不要忘记,setter和getters可以是私有的,甚至对于内部管理的属性而言,拥有一个管理该属性的setter可能也是可取的.例如,获取一个连接字符串并将其传递给HttpURLConnection的设置器.

Don't forget that setters and getters can be private and even for internally managed properties, having a setter that manages the property may be desirable. For example, taking a connection string and passing it to the setter for HttpURLConnection.

也请注意:

Allen Holub的文章为什么getter和setter方法是邪恶的似乎是OP推理的源头,但在我看来,本文在解释其观点方面做得很差.

Allen Holub's article Why getter and setter methods are evil seems to be the source of OP's reasoning but, in my opinion, the article does a poor job of explaining its point.

添加了摘要
拼写更正

Added summary
Edit 2: spelling corrections

这篇关于如何避免getter和setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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