英语不是我的母语,我无法理解如何正确编写指定的示例。当你说一些聚合复数宾语的东西时,比如“集邮”,你可以说:“集邮”,我说对了吗?如果你会说“集邮”,它将意味着一些“集合”,这是一个单一的“邮票”。
但是我经常看到名字像"ItemList“的类-这不是意味着这样的类是一个列表,是其他东西的一个项目吗?这样的样本更加耀眼:
class ItemList: List<Item>难道不是非得这样吗?
class ItemsList: List<Item>为什么很少这样写呢?或者是某种编程语言的命名约定?或者仅仅是合适的英语句子?:)
发布于 2012-03-20 18:31:28
我发现ItemList更具描述性。它是Item类型的对象的list。
另一方面,如果您有一个Item对象的集合,您可以将其称为Items,而ItemsList将指定一个集合的list。
发布于 2012-03-20 18:32:28
class Items: List<Item>而且更具可读性,通常我会做一个typedef
// C++98
typedef List<Item> Items;
// since C++11 you can write it also this way, and I prefer this:
using Items = List<Item>;考虑一个更专业的隐藏列表的类通常是的好实践(干净的代码)。“偏好通过聚合而不是继承来组合”是OOP中的常见习语(引用萨特/亚历山大: C++编码标准: 101规则、指南和最佳实践)
集合的封装在大多数情况下是一种很好的做法,它可以降低调用代码的复杂性:
class Encapsulation
{
Items m_items;
public:
void AddItem(const Item&);
void RemoveAllItems();
// ... all the functions about managing the List that would be otherwise boilerplate code spread all over your codebase
}https://stackoverflow.com/questions/9784720
复制相似问题