当前位置:   article > 正文

Go语言学习笔记—golang接口与实现面向对象特性_golang 接口实现 及对象

golang 接口实现 及对象

视频来源:B站《golang入门到项目实战 [2022最新Go语言教程,没有废话,纯干货!]》

文章为自己整理的学习笔记,侵权即删,谢谢支持!


一 golang接口简介

go语言的接口,是一种新的类型定义,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。

1.1 语法

语法格式和方法非常类似。

/* 定义接口 */
type interface_name interface {
   
    method_name1 [return_type]
    method_name2 [return_type]
    method_name3 [return_type]
    ...
    method_namen [return_type]
}
 
/* 定义结构体 */
type struct_name struct {
   
    /* variables */
}
 
/* 实现接口方法 */
func (struct_name_variable struct_name) method_name() [return_type] {
   
    /* 方法实现 */
}
...
/* 实现接口方法 */
func (struct_name_variable struct_name) method_name() [return_type] {
   
    /* 方法实现 */
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在接口定义中定义,若干个空方法。这些方法都具有通用性。

1.2 实例演示

定义一个USB接口,有读read和写write两个方法,再定义一个电脑Computer和一个手机Mobile来实现这个接口。

定义USB接口:

type USB interface {
   
    read()
    write()
}
  • 1
  • 2
  • 3
  • 4
  • 5

定义Computer结构体:

type Computer struct {
   
}
  • 1
  • 2
  • 3

定义Mobile结构体:

type Mobile struct {
   
}
  • 1
  • 2
  • 3

Computer实现USB接口方法:

func (c Computer) read() {
   
    fmt.Println("computer read...")
}
 
func (c Computer) write() {
   
    fmt.Println("computer write...")
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Mobile实现USB接口方法:

func (c Mobile) read() {
   
    fmt.Println("mobile read...")
}
 
func (c Mobile) write() {
   
    fmt.Println("mobile write...")
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

测试:

package main
 
import "fmt"
 
type USB interface {
   
    read()
    write()
}
 
type Computer struct {
   
}
 
type Mobile struct {
   
}
 
func (c Computer) read() {
   
    fmt.Println("computer read...")
}
 
func (c Computer) write() {
   
    fmt.Println("computer write...")
}
 
func (c Mobile) read() {
   
    fmt.Println("mobile read...")
}
 
func (c Mobile) write() {
   
    fmt.Println("mobile write...")
}
 
func main() {
   
    c := Computer{
   }
    m := Mobile{
   }
 
    c.read()
    c.write()
    m.read()
    m.write()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

运行结果:

computer read...
computer write...
mobile read...
mobile write...
  • 1
  • 2
  • 3
  • 4

1.3 注意事项

  1. 接口本身不能创建实例,但是可以指向一个实现了该接口的自定义类型的变量(实例)

  2. 接口中所有的方法都没有方法体,即都是没有实现的方法

  3. 在 Golang 中,一个自定义类型需要将某个接口的所有方法都实现,我们说这个自定义类型实现了该接口

  4. 一个自定义类型只有实现了某个接口,才能将该自定义类型的实例(变量)赋给接口类型

  5. 只要是自定义数据类型,就可以实现接口,不仅仅是结构体类型。

  6. 一个自定义类型可以实现多个接口

  7. Golang 接口中不能有任何变量

  8. 实现接口必须实现接口中的所有方法

    下面我们定义一个OpenClose接口,里面有两个方法open和close,定义个Door结构体,实现其中一个方法。

package main
 
import "fmt"
 
type OpenClose interface {
   
    open()
    close()
}
 
type Door struct {
   
}
 
func (d Door) open() {
   
    fmt.Println("open door...")
}
 
func main() {
   
    var oc OpenClose
    oc = Door{
   <
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/907795
推荐阅读
相关标签
  

闽ICP备14008679号