什么时候使用委托而不是继承?

发布于 2024-07-20 01:11:12 字数 1436 浏览 5 评论 0原文

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

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

发布评论

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

评论(6

百思不得你姐 2024-07-27 01:11:12

当您想要“复制”/公开基类的 API 时,可以使用继承。
当您只想“复制”功能时,请使用委派。

一个例子:
您想从列表中创建一个堆栈。 栈只有pop、push和peek三种。 鉴于您不希望在堆栈中使用push_back、push_front、removeAt 等功能,您不应该使用继承。

When you want to "copy"/Expose the base class' API, you use inheritance.
When you only want to "copy" functionality, use delegation.

One example of this:
You want to create a Stack out of a List. Stack only has pop, push and peek. You shouldn't use inheritance given that you don't want push_back, push_front, removeAt, et al.-kind of functionality in a Stack.

野鹿林 2024-07-27 01:11:12

他们彼此没有任何关系。 委派是一种行为。 继承是一种模型技术。

继承是为了建模“is-a”。 计算机“是一个”电子系统。

委托是方法提供结果的方式。 有时,一个对象会将工作委托给另一个对象。 委派可以通过任何关系进行——您可以委派给超类、组合或聚合的成员或任何关系。

They have nothing to do with each other. Delegation is a behavior. Inheritance is a model technique.

Inheritance is for modeling "is-a". A computer "is-a" electronic system.

Delegation is how methods provide results. Sometimes one object will delegate work to another object. Delegation can be via any relationship -- you can delegate to a superclass, to a member of a composite or aggregate, or any relationship.

挽梦忆笙歌 2024-07-27 01:11:12

您可以使用多个内部类实例的委托来将它们的功能简化为公共分组。 例如,如果您的语言没有实现多重继承,您可以从其中一个基础继承并包装另一个基础,将您想要公开的功能委托给底层实现。 继承还将您的类与您继承的类的层次结构联系起来,就像使用委托一样,您可以在自己的层次结构中保留自己的位置并将调用委托给另一个类。

You may use delegation to multiple internal class instances to simplify their functionality into a common grouping. If your language doesn't implement multiple inheritance for instance you may inherit from one of the bases and wrap the other, delegating the functionality you want to expose to the underlying implementation. Inheritance also ties your class into the hierarchy of classes you are inheriting from where as with delegation you may keep your place in your own hierarchy and delegate calls to another.

绅士风度i 2024-07-27 01:11:12

假设您的类称为 B,并且派生/委托的类称为 A,那么

以下是使用继承或委托时的一些示例:
如果

  • 你想表达关系(is-a)那么你想使用继承。
  • 如果您希望能够将您的类传递给需要 A 的现有 API,那么您需要使用继承。
  • 你想增强A,但A是最终的,不能进一步子类化,那么你需要使用组合和委托。

Assume your class is called B and the derived/delegated to class is called A then

Here are some examples when inheritance or delegation are being used:
If

  • you want to express relationship (is-a) then you want to use inheritance.
  • you want to be able to pass your class to an existing API expecting A's then you need to use inheritance.
  • you want to enhance A, but A is final and can no further be subclassed then you need to use composition and delegation.
七颜 2024-07-27 01:11:12

我同意@Anzurio。 用简单的话来解释一下:

仅当您觉得新类是现有类的自然扩展时才使用继承。 更喜欢将组合/委托用于所有其他目的,例如使用特定方法等。

I agree with @Anzurio. Just to explain it in simple words:

Use inheritance only when you feel like the new class is a natural extension of the existing class. Prefer to use composition/delegation for all other purposes like using a specific method, etc.

我的痛♀有谁懂 2024-07-27 01:11:12

在我看来,当需要使用时可以调用委托,并且继承是嵌入的。

in_threads gem 为例,它使用 InThreads 委托器来实现任何 Ruby Enumerable 模块的线程。 这样,只需在数组上调用 in_threads 方法,它将在线程中运行。

In my opinion, delegation can be called when it's time to use and inheritance is embedded ever.

Take the in_threads gem for example, it use InThreads delegator to implement the threads for any Ruby Enumerable module. With this, only call in_threads methods on a array, it will run in threads.

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