4

5.4 Go语言中自定义类型与结构体(Struct)

 1 year ago
source link: https://blog.csdn.net/xiaoquqi/article/details/125984062
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

5.4 Go语言中自定义类型与结构体(Struct)

Go是面向对象的语言吗

在很多文章中都提到,Go语言中的struct某种意义就是对应其他语言中的class,就该问题特意阅读了Go语言官网的常见问题,有这样的描述

Is Go an object-oriented language?

Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).

Also, the lack of a type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.

但是又舍弃了很多类在继承的特性,让Go语言变得更轻。

文章知识点与官方知识档案匹配,可进一步学习相关知识

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK