“C 类用 B 扩展 A 类”和“C 类扩展 A 类”之间有什么区别?以及“C类延伸B”当特质 B 延伸特质 A 时

发布于 2024-12-25 03:54:26 字数 237 浏览 2 评论 0原文

当两个特质被这样定义时,

trait A
trait B extends A

这两者有什么区别。

class C extends B
class D extends A with B

我认为 C 类或 D 类没有必要扩展特征 A,因为特征 B 已经扩展了特征 A。

为什么经常写成“D 类用 B 扩展 A”?

When two traits are defined like this,

trait A
trait B extends A

what is the difference between these two.

class C extends B
class D extends A with B

I do not think it is necessary for class C or D to extends trait A since trait B already extends trait A.

Why is this often written "class D extends A with B" ?

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

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

发布评论

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

评论(1

优雅的叶子 2025-01-01 03:54:26

这是一个非常好的问题...我会尝试以主观的回应来回答。

我想当层次结构线性化时,扩展 A mixin 与 B 很重要,在这种特殊情况下,没有区别,但如果你 mixin D 与另一个特征 E 重新实现 A 中的一些(但不是全部)函数,这些函数在 B 中不适合您在 D 中的需求>。因此

class D extends A with E with B

,在这种情况下,我们保留 D is a A 的含义

It's a pretty good question... I'll try to answer with a subjective response.

I guess that extends A mixin with B is important when the hierarchy will be linearized, in this particular case there is no differences but what if you mixin D with another trait E that reimplements some (but not all) functions from A which aren't well advised in B for your needs in D. So you'd have

class D extends A with E with B

Moreover in that case, we keep the meaning that D is a A

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