在 Go 中嵌入而不是继承 [英] Embedding instead of inheritance in Go

查看:29
本文介绍了在 Go 中嵌入而不是继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您对这个设计决定有何看法?它有什么优点,有什么缺点?

What is your opinion of this design decision? What advantages does it have and what disadvantages?

链接:

推荐答案

在评论中,您想知道嵌入的想法是否足以完全取代继承".我会说这个问题的答案是是".几年前,我使用一个名为 Snit,它使用组合和委托来排除继承.Snit 与 Go 的方法仍有很大不同,但在这一方面,它们有一些共同的哲学基础.它是一种将功能和职责结合在一起的机制,而不是类的层次结构.

In a comment, you wondered if the embedding idea was enough to "replace inheritance completely". I would say the answer to that question is "yes". A few years ago I played very briefly with a Tcl OO system called Snit, which used composition and delegation to the exclusion of inheritance. Snit is still vastly different from Go's approach, but in that one respect they have some common philosophical ground. It's a mechanism for joining together pieces of functionality and responsibility, not a hierarchy for the classes.

正如其他人所说,这实际上是关于语言设计者想要支持什么样的编程实践.所有这些选择都有其优缺点.我不认为最佳实践"是一个在这里一定适用的短语.我们最终可能会看到有人为 Go 开发一个继承层.

As others have stated, it's really about what kind of programming practices the language designers want to support. All such choices come with their own pros and cons; I don't think "best practices" is a phrase that necessarily applies here. We will probably see someone develop an inheritance layer for Go eventually.

(对于任何熟悉 Tcl 的读者,我觉得 Snit 比 [incr Tcl] 更接近于语言的感觉".Tcl 是关于委托的,在至少是我的思维方式.)

(For any readers familiar with Tcl, I felt Snit to be a slightly closer match to the "feel" of the language than [incr Tcl] was. Tcl is all about the delegation, at least to my way of thinking.)

这篇关于在 Go 中嵌入而不是继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆