类中应放置哪些函数

发布于 2024-10-07 08:17:55 字数 162 浏览 5 评论 0原文

如果我有一个函数(例如 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 技术交流群。

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

发布评论

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

评论(4

你在我安 2024-10-14 08:17:55

这里涉及到一个权衡。使用成员函数可以让您:

  • 重写派生类中的实现,这样即使在通用房间中没有可用的橱柜,弄乱厨房也可能涉及到扔掉橱柜。
  • 决定稍后需要访问私有变量,而不必重构使用该函数的所有代码。
  • 使函数成为接口的一部分,以便一段代码可能要求其参数是可混乱的。

使用外部函数可以让您:

  • 使该函数通用,以便您可以将其同样应用于房间、仓库和石油钻井平台(如果它们提供混乱所需的成员函数)。
  • 保持类签名较小,以便为单元测试(或不同的实现)创建模拟版本变得更容易。
  • 无需检查该函数的代码即可更改类实现。

没有真正的鱼与熊掌兼得的方法,所以你必须做出选择。一个常见的面向对象决策是让一切都成为方法(除非明显愚蠢)并牺牲后三点,但这并不意味着您应该在所有情况下都这样做。

There's a tradeoff involved here. Using a member function lets you:

  • Override the implementation in derived classes, so that messing up a kitchen could involve trashing the cupboards even if no cupboards are available in a generic room.
  • Decide that you need to access private variables later on, without having to refactor all the code that uses the function.
  • Make the function part of an interface, so that a piece of code may require that its argument be mess-up-able.

Using an external function lets you:

  • Make that function generic, so that you may apply it to rooms, warehouses and oil rigs equally (if they provide the member functions required for messing up).
  • Keep the class signature small, so that creating mock versions for unit testing (or different implementations) becomes easier.
  • Change the class implementation without having to examine the code for that function.

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.

失而复得 2024-10-14 08:17:55

一类对象的任何行为都应该编写为实例方法。

所以 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 be cleanUp or paint, etc...

当梦初醒 2024-10-14 08:17:55

忽略哪种语言,我想我的第一个问题是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).

倾城泪 2024-10-14 08:17:55

我以 "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.

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