Go-interface基本概念 - 温昀 - 博客园

1. 基本介绍:

interface类型可以定义一组方法,但是这些不需要实现。并且interface不能包含任何变量。到某个自定义类型要使用的时候,再根据具体情况把这些方法写出来。

例:

+ View Code?

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

51

52

53

54

55

56

57

58

59

60

61

62

package main

import (

"fmt"

)

type Usb interface {

//声明两个方法

Start()

Stop()

}

type Phone struct {

}

type Camera struct {

}

type Computer struct {

}

//让phone实现USB接口的方法

func (p Phone) Start() {

fmt.Println(`"手机开始工作……"`)

}

func (p Phone) Stop() {

fmt.Println(`"手机停止工作……"`)

}

//让camera实现USB接口的方法

func (c Camera) Start() {

fmt.Println(`"相机开始工作……"`)

}

func (c Camera) Stop() {

fmt.Println(`"相机停止工作……"`)

}

// Computer working方法,接收一个usb接口类型变量

// 只要是实现了USB接口

// 实现USB接口,就是指实现了USB接口声明的所有方法

func (c Computer) Working(usb Usb) {

//通过USB接口来调用Start和Stop方法

usb.Start()

usb.Stop()

}

func main() {

fmt.Println(`"Hello"`)

//测试

//1.创建结构体变量

computer := Computer{}

phone := Phone{}

camera := Camera{}

//关键点

//把phone传给接口,接口去调用start、stop

computer.Working(phone)

//把camera传给接口,接口去调用start、stop

computer.Working(camera)

}

2. 基本语法:

定义一个接口:

+ View Code?

1

2

3

4

5

6

type Randomer interface {

// 声明方法

method1(参数列表)返回值列表

method2(参数列表)返回值列表

// ……

}

实现接口所有方法:

+ View Code?

1

2

3

4

5

6

func (t 自定义类型)method1(参数列表)返回值列表 {

//方法实现

}

func (t 自定义类型)method2(参数列表)返回值列表 {

//方法实现

}

3.小结:

(1)接口里的所有方法都没有方法体,即接口的方法都是没有实现的方法。接口体现了程序设计的多态和高内聚低耦合的思想。

(2)Go中的接口,不需要显示的实现。只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。因此,Go中没有implement关键字样。

(3)Go实现接口与方法有关,与接口本身叫什么名字没有特别大的关系。变量需要实现接口所有的方法。

4.注意事项和细节

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

+ View Code?

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

package main

import (

"fmt"

)

type AInterface interface {

Say()

}

type Stu struct {

Name string

}

func (stu Stu) Say() {

fmt.Println(`"Student say"`)

}

func main() {

var stu Stu //结构体变量,实现了Say(),实现了AInterface

// 接口本身不能创建实例

// var a AInterface

// a.Say()

var a AInterface = stu

a.Say()

}

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

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

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

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

+ View Code?

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

package main

import (

"fmt"

)

type AInterface interface {

Say()

}

type interger int

func (i interger) Say() {

fmt.Println(`"interger Say i ="`, i)

}

func main() {

// 接口本身不能创建实例

// var a AInterface

// a.Say()

var i interger = 10

i.Say()

var b AInterface = i

b.Say()

}

(6)一个自定义类型可以实现多个接口。

+ View Code?

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

package main

import (

"fmt"

)

type AInterface interface {

Say()

}

type BInterface interface {

Hello()

}

type Monster struct {

}

func (m Monster) Hello() {

fmt.Println(`"Monster Hello ……"`)

}

func (m Monster) Say() {

fmt.Println(`"Monster Say ……"`)

}

func main() {

// 接口本身不能创建实例

// var a AInterface

// a.Say()

//Monster实现了AInterface 和 BInterface

var monster Monster

var a2 AInterface = monster

var b2 BInterface = monster

a2.Say()

b2.Hello()

}

(7)Go接口不能有任何变量。

(8)一个接口可以继承多个别的接口,这时如果要实现这个接口必须实现它继承的所有接口的方法。在低版本的Go编辑器中,一个接口继承其他多个接口时,不允许继承的接口有相同的方法名。比如A接口继承B、C接口,B、C接口的方法名不能一样。高版本的Go编辑器没有相关问题。

+ View Code?

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

package main

import (

"fmt"

)

type AInterface interface {

test01()

}

type BInterface interface {

test02()

}

type CInterface interface {

AInterface

BInterface

test03()

}

type Stu struct {

}

func (stu Stu) test01() {

fmt.Println(`"test01"`)

}

func (stu Stu) test02() {

fmt.Println(`"test02"`)

}

func (stu Stu) test03() {

fmt.Println(`"test03"`)

}

func main() {

// 接口本身不能创建实例

// var a AInterface

// a.Say()

//Monster实现了AInterface 和 BInterface

var stu Stu

var c CInterface = stu

c.test01()

}

(9)interface类型默认是一个指针(引用类型),如果没有对interface初始化就使用,那么会输出nil。

(10)空接口interface{}没有任何方法,所以所有类型都实现了空接口,即我们可以把任何一个变量赋给空接口类型。

+ View Code?

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

package main

import (

"fmt"

)

// 空接口

type Temp interface {

}

type Stu struct {

}

func main() {

// 接口本身不能创建实例

// var a AInterface

// a.Say()

//Monster实现了AInterface 和 BInterface

var stu Stu

var t Temp = stu

fmt.Println(t)

var t2 interface`{} = stu`

fmt.Println(t2)

var t3 interface`{}`

var num1 float64 = 8.8

t3 = num1

fmt.Println(t3)

}

以上资料来源:https://www.bilibili.com/video/BV1ME411Y71o?p=215&spm_id_from=pageDriver


原网址: 访问
创建于: 2022-08-17 22:02:25
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论