gpt4 book ai didi

Go 中的继承

转载 作者:IT王子 更新时间:2023-10-29 01:16:04 26 4
gpt4 key购买 nike

为什么 Go 没有类型继承。

(定义一个对象的类时,定义的任何子类都可以继承一个或多个通用类的定义的概念)

最佳答案

这是由语言创建者在 F.A.Q. 中回答的:

Object-oriented programming, at least in the best-known languages, involves too much discussion of the relationships between types, relationships that often could be derived automatically. Go takes a different approach.

Rather than requiring the programmer to declare ahead of time that two types are related, in Go a type automatically satisfies any interface that specifies a subset of its methods. Besides reducing the bookkeeping, this approach has real advantages. Types can satisfy many interfaces at once, without the complexities of traditional multiple inheritance. Interfaces can be very lightweight—an interface with one or even zero methods can express a useful concept. Interfaces can be added after the fact if a new idea comes along or for testing—without annotating the original types. Because there are no explicit relationships between types and interfaces, there is no type hierarchy to manage or discuss.

另请参阅:Composition over inheritance principle .

关于Go 中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34151961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com