教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 【Go语言入门系列】(八)Go语言是不是面向对象语言?

【Go语言入门系列】(八)Go语言是不是面向对象语言?

发布时间:2021-12-18   编辑:jiaochengji.com
教程集为您提供【Go语言入门系列】(八)Go语言是不是面向对象语言?等资源,欢迎您收藏本站,我们将为您提供最新的【Go语言入门系列】(八)Go语言是不是面向对象语言?资源

【Go语言入门系列】前面的文章:

<ul><li>【Go语言入门系列】(五)指针和结构体的使用</li><li>【Go语言入门系列】(六)再探函数</li><li>【Go语言入门系列】(七)如何使用Go的方法?</li></ul><h1>1. Go是面向对象的语言吗?</h1>

在【Go语言入门系列】(七)如何使用Go的方法?这一文中已经介绍了方法的概念,但这个方法实际上并不是面向对象中的方法。方法实际上是用户给其定义的类型的增加的新行为,实际上也是个函数。

关于这个问题,官方文档中有回答:

<blockquote>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拥有类型和方法,也允许面向对象风格的编程,但它没有类型层级。 在Go中“接口”的概念提供了不同的方法,我们相信它易于使用且在某些方面更通用。 也有一些在其它类型中嵌入类型的方法,来提供类似(而非完全相同)的东西进行子类化。 此外,Go中的方法比C 或Java中的更通用:它们可被定义为任何种类的数据。 甚至是像普通的“未装箱”整数这样的内建类型。它们并不受结构(类)的限制。

此外,类型层级的缺失也使Go中的“对象”感觉起来比C 或Java的更轻量级

</blockquote>

有了这个回答,下面介绍的“继承”和“重写”的概念并不是严格的面向对象中的继承和重写的概念。这里只借用这两个名词来表示Go的两种特性。

<h1>2.“继承”</h1>

在面向对象中,继承是子类和父类之间的关系,子类会继承父类的公有成员变量和成员方法。

前面提到过,Go允许面向对象风格的编程。那Go如何“继承”呢?

<h2>2.1.“继承”字段</h2>

在【Go语言入门系列】(五)指针和结构体的使用这一文中,介绍了匿名字段(也叫嵌入字段):

<pre><code class="go">package main import "fmt" type people struct { name string age int } type student struct { people school string } func (s student) say() { fmt.Printf("我是%s,今年%d岁了,在%s上学。", s.name, s.age, s.school) } func main() { stu := student{people{"行小观", 1}, "阳光小学"} stu.say() }</code></pre>

运行:

<pre><code class="go">我是行小观,今年1岁了,在阳光小学上学</code></pre>

结构体<code>student</code>中有匿名字段<code>people</code>,所以<code>student</code>就有了<code>people</code>的<code>name</code>和<code>age</code>字段。当匿名字段是一个结构体时,那么该结构体的全部字段都会被引入当前的结构体中。这是不是很像面向对象中的继承?子类继承父类的公有成员变量。

考虑下面一个问题,如果<code>student</code>和<code>people</code>中都有<code>name</code>字段,那么访问时,Go语言是如何处理的?

<pre><code class="go">package main import "fmt" type people struct { name string //人名 age int } type student struct { people name string //学生名 school string } func main() { stu := student{people{"李二狗", 1}, "李向前", "阳光学校"} fmt.Println(stu.name) //李向前 fmt.Println(stu.people.name) //李二狗 }</code></pre>

此时就出现了字段冲突,Go会先访问外层的字段。比如,<code>stu.name</code>是<code>李向前</code>(外层),<code>stu.people.name</code>是<code>李二狗</code>(内层)。

<h2>2.2.“继承”方法</h2>

我们通过接收者把函数绑定到结构体类型上,这样的函数称为方法,方法就在概念上属于了接收者对应的结构体。

上面通过结构体中匿名字段实现了“继承”字段的效果,对于方法来说,同样可以。下面是一个实例:

<pre><code class="go">package main import "fmt" type people struct { name string age int } type student struct { people school string } type programmer struct { people language string } func (p people) say() { fmt.Printf("我是%s,今年%d岁了,和我一起学习Go语言吧!\n", p.name, p.age) } func main() { stu := student{people{"行小观", 1}, "阳光小学"} stu.say() prom := programmer{people{"张三", 1}, "蓝天建筑有限公司"} prom.say() }</code></pre>

运行:

<pre><code class="go">我是行小观,今年1岁了,和我一起学习Go语言吧! 我是张三,今年1岁了,和我一起学习Go语言吧!</code></pre>

<code>say()</code>方法的接收者是<code>people</code>类型,而结构体<code>student</code>和<code>programmer</code>中都有匿名字段<code>people</code>,所以<code>stu</code>和<code>prom</code>都能调用<code>say()</code>方法。这是不是很像面向对象语言中子类继承父类的公有方法?

<h1>3. “重写”</h1><h2>3.1. “重写”字段</h2>

前面已经介绍了如果结构体和作为其字段的结构体的字段冲突了如何处理。有了这个特性,我们就可以“重写”字段。

<pre><code class="go">package main import "fmt" type people struct { name string //乳名 age int } type student struct { people name string //大名 school string } func (s student) say() { fmt.Printf("我是%s,今年%d岁了,和我一起学习Go语言吧!\n", s.name, s.age) } func main() { stu := student{people{"李二狗", 1}, "李向前","阳光小学"} stu.say() }</code></pre>

运行:

<pre><code class="go">我是李向前,今年1岁了,和我一起学习Go语言吧!</code></pre>

李二狗是乳名,李向前才是大名。自我介绍时说的是大名李向前。如果需要乳名,则使用<code>stu.people.name</code>。

<h2>3.2.“重写”方法</h2>

下面是一个实例:

<pre><code class="go">package main import "fmt" type people struct { name string age int } type student struct { people school string } type programmer struct { people language string } func (p people) say() { //people的say方法 fmt.Printf("我是%s,今年%d岁了,和我一起学习Go语言吧!\n", p.name, p.age) } func (s student) say() { //student重写people的say方法 fmt.Printf("我是%s,是个学生,今年%d岁了,我在%s上学!\n", s.name, s.age, s.school) } func (p programmer) say() { //programmer重写people的say方法 fmt.Printf("我是%s,是个程序员,今年%d岁了,我使用%s语言!\n", p.name, p.age, p.language) } func main() { stu := student{people{"李向前", 1}, "阳光小学"} stu.say() prmger := programmer{people{"张三", 1}, "Go"} prmger.say() }</code></pre>

运行:

<pre><code class="go">我是李向前,是个学生,今年1岁了,我在阳光小学上学! 我是张三,是个程序员,今年1岁了,我使用Go语言!</code></pre>

<code>student</code>和<code>programmer</code>“继承”了<code>people</code>的<code>say()</code>方法,但是不合适,于是各自“重写”了<code>say()</code>方法。

看到这里,你就理解了官方文档中的那两句话是什么意思了。

“尽管Go拥有类型和方法,也允许面向对象风格的编程,但它没有类型层级”

“类型层级的缺失也使Go中的“对象”感觉起来比C 或Java的更轻量级”

<h1>作者简介</h1><blockquote>

我是行小观,我会在公众号『行人观学』中持续更新Java、Go、数据结构和算法、计算机基础等相关文章。


本文章收录于系列文章「Go语言入门系列」,本系列从Go语言基础开始介绍,适合从零开始的初学者。


欢迎关注,我们一起踏上编程的行程。

如有错误,还请指正。

</blockquote> 到此这篇关于“【Go语言入门系列】(八)Go语言是不是面向对象语言?”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
想系统学习GO语言(Golang
【Go语言入门系列】(八)Go语言是不是面向对象语言?
Go语言学习3----Go语言特色
Go语言发展历史、核心、特性及学习路线
Golang 基础教程
从零学习 Go 语言(24):理解 Go 语言中的 goroutine
Go语言的主要特性和发展影响
Go 语言十年而立,Go2 蓄势待发
Go语言开发必读书目,从菜鸟到大牛必备
你好,Go语言

[关闭]
~ ~