类中应放置哪些函数
如果我有一个函数(例如 messUp
)不需要访问类的任何私有变量(例如 room
),我应该在类中编写该函数吗? >room.messUp() 还是像 messUp(room)
这样的外部版本?看来第二个版本对我来说读起来更好。
If I have a function (say messUp
that does not need to access any private variables of a class (say room
), should I write the function inside the class like room.messUp()
or outside of it like messUp(room)
? It seems the second version reads better to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里涉及到一个权衡。使用成员函数可以让您:
使用外部函数可以让您:
没有真正的鱼与熊掌兼得的方法,所以你必须做出选择。一个常见的面向对象决策是让一切都成为方法(除非明显愚蠢)并牺牲后三点,但这并不意味着您应该在所有情况下都这样做。
There's a tradeoff involved here. Using a member function lets you:
Using an external function lets you:
There's no real way to have your cake and eat it too, so you have to make choices. A common OO decision is to make everything a method (unless clearly idiotic) and sacrifice the three latter points, but that doesn't mean you should do it in all situations.
一类对象的任何行为都应该编写为实例方法。
所以
room.messUp()
是执行此操作的 OO 方法。无论
messUp
是否必须访问类的任何私有成员,都无关紧要,事实上,它是房间的行为,表明它是一个实例方法,就像cleanUp 或
paint
等...Any behaviour of a class of objects should be written as an instance method.
So
room.messUp()
is the OO way to do this.Whether
messUp
has to access any private members of the class or not, is irrelevant, the fact that it's a behaviour of the room, suggests that it's an instance method, as would becleanUp
orpaint
, etc...忽略哪种语言,我想我的第一个问题是messUp是否与任何其他函数相关。如果你有一组相关的函数,我倾向于将它们放在一个类中。
如果它们不访问任何类变量,那么您可以将它们设为静态。这样,无需创建类的实例即可调用它们。
除此之外,我还会关注语言。在某些语言中,每个函数都必须是某个类的方法。
最后,我不认为这有什么大的区别。 OOP 只是一种帮助组织应用程序数据和逻辑的方法。如果你接受它,那么你会选择 room.messUp() 而不是messUp(room)。
Ignoring which language, I think my first question is if messUp is related to any other functions. If you have a group of related functions, I would tend to stick them in a class.
If they don't access any class variables then you can make them static. This way, they can be called without needing to create an instance of the class.
Beyond that, I would look to the language. In some languages, every function must be a method of some class.
In the end, I don't think it makes a big difference. OOP is simply a way to help organize your application's data and logic. If you embrace it, then you would choose room.messUp() over messUp(room).
我以 "C++ 编码标准:101 条规则、指南、 Sutter 和 Alexandrescu 的《最佳实践》,以及 Bob Martin 的 SOLID。我当然同意他们的观点;-)。
如果消息/函数与您的类没有太多交互,您应该将其作为标准的普通函数,以您的类对象作为参数。
你不应该用与班级不密切相关的行为来污染你的班级。
这是为了遵守单一职责原则:你的类应该保持简单,旨在最精确目标。
但是,如果您认为您的消息/函数与您的对象内部密切相关,那么您应该将其包含为类的成员函数。
i base myself on "C++ Coding Standards: 101 Rules, Guidelines, And Best Practices" by Sutter and Alexandrescu, and also Bob Martin's SOLID. I agree with them on this point of course ;-).
If the message/function doesnt interract so much with your class, you should make it a standard ordinary function taking your class object as argument.
You should not polute your class with behaviours that are not intimately related to it.
This is to repect the Single Responsibility Principle: Your class should remain simple, aiming at the most precise goal.
However, if you think your message/function is intimately related to your object guts, then you should include it as a member function of your class.