首页 > 其他分享 >Go笔记(十二):接口

Go笔记(十二):接口

时间:2023-05-05 21:11:28浏览次数:38  
标签:name fmt 笔记 接口 func Go type struct

1、接口的声明

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

语法详情如下:

/* 定义接口 */
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_name1() [return_type] {
   /* 方法实现 */
}
...
func (struct_name_variable struct_name) method_namen() [return_type] {
   /* 方法实现*/
}

  示例代码如下:

 1 package main
 2 
 3 import "fmt"
 4 
 5 // 不同结构体的接受者
 6 type Plane struct {
 7    name string
 8 }
 9 
10 // 要实现某个接口,必须实现该接口的所有方法
11 type Bird struct {
12    name string
13    color string
14 }
15 
16 // 定义接口
17 type Flying interface {
18    // 定义接口方法
19    takeoff()
20    descend()
21 }
22 
23 // 方法的接收者是 Bird
24 func (bd Bird) takeoff() {
25    fmt.Printf("bd.name: %v, bd.color: %v  take off \n", bd.name, bd.color)
26 }
27 func (bd Bird) descend() {
28    fmt.Printf("bd.name: %v, bd.color: %v  descend \n", bd.name, bd.color)
29 }
30 
31 // 方法的接收者是 Plane
32 func (pe Plane) takeoff() {
33    fmt.Printf("pe.name: %v takeoff \n", pe.name)
34 }
35 func (pe Plane) descend() {
36    fmt.Printf("pe.name: %v descend \n", pe.name)
37 }
38 
39 func main() {
40    bd := Bird{"猫头鹰", "黑色"}
41    bd.takeoff()
42    bd.descend()
43    fmt.Printf("--------------\n")
44    pe := Plane{"爱国者"}
45    pe.takeoff()
46    pe.descend()
47 }

2、接口接受者类型

2.1、值类型

 1 package main
 2 
 3 import "fmt"
 4 
 5 // 定义接口
 6 type Animal interface {
 7    eat(name string)
 8 }
 9 // 声明结构体
10 type Cat struct {
11    name string
12 }
13 // 值类型接受者
14 func (cat Cat) eat(name string) {
15    cat.name = cat.name + "-v"
16    fmt.Printf("cat.name: %v eating \n", cat.name)
17 }
18 
19 // 接口值类型接受者
20 func main() {
21    cat01 := Cat{
22       name: "pter",
23     }
24    fmt.Printf("cat01: %v\n", cat01)
25    cat01.eat(cat01.name)
26    fmt.Printf("cat01: %v\n", cat01)
27 }

2.2、指针类型

 1 package main
 2 
 3 import "fmt"
 4 
 5 // 定义接口
 6 type Animal interface {
 7    sleep(name string)
 8 }
 9 // 声明结构体
10 type Cat struct {
11    name string
12 }
13 
14 // 指针类型接受者
15 func (cat *Cat) sleep(name string) {
16    cat.name = cat.name + "-p"
17    fmt.Printf("cat.name: %v sleep \n", cat.name)
18 }
19 
20 // 接口指针类型接受者
21 func main() {
22    cat := Cat{
23       name: "pter",
24     }
25    fmt.Printf("cat: %v\n", cat)
26    cat.sleep(cat.name)
27    fmt.Printf("cat: %v\n", cat)
28 }

3、接口与类型的关系

3.1、一个类型实现多个接口

 1 package main
 2 
 3 import "fmt"
 4 
 5 // 有声音的接口
 6 type Audible interface {
 7    playMusic()
 8 }
 9 // 可观看的接口
10 type Viewable interface {
11    viewMovie()
12 }
13 // 结构体
14 type Computer struct{}
15 // 接收者为 Computer 的方法
16 func (c Computer) playMusic() {
17    fmt.Println("play music...")
18 }
19 func (c Computer) viewMovie() {
20    fmt.Println("view movie...")
21 }
22 
23 // 一个类型实现多个接口
24 func main() {
25    computer := Computer{}
26    computer.playMusic()
27    computer.viewMovie()
28 }

3.2、多个类型实现同一接口

 1 package main
 2 
 3 import "fmt"
 4 
 5 type BC struct {
 6    kind string
 7 }
 8 type BM struct {
 9    kind string
10 }
11 type Car interface {
12    run()
13 }
14 func (m BM) run() {
15    fmt.Println(" 宝马在高速行驶 ")
16 }
17 func (c BC) run() {
18    fmt.Println(" 奔驰在高速行驶 ")
19 }
20 
21 func main() {
22    m := BM{}
23    c := BC{}
24    m.run()
25    c.run()
26 }

3.3、接口嵌套

 1 package main
 2 
 3 import "fmt"
 4 
 5 // 爬行的
 6 type Crawling interface {
 7    crawl()
 8 }
 9 // 游泳的
10 type Natatorial interface {
11    swim()
12 }
13 // 接口组合
14 type AmphibiousAnimals interface {
15    Crawling
16    Natatorial
17 }
18 // 定义结构体
19 type tortoise struct{}
20 // 实现接口方法
21 func (t tortoise) crawl() {
22    fmt.Println(" tortoise crawl ")
23 }
24 // 实现接口方法
25 func (t tortoise) swim() {
26    fmt.Println(" tortoise swim ")
27 }
28 
29 func main() {
30    t := tortoise{}
31    t.crawl()
32    t.swim()
33 }

4、模拟接口设计原则

  面向对象的OCP设计原则,是可复用设计的基石,"开闭"原则(Open-Closed Principle),对拓展是开放的,对修改是关闭的。

  Go语言虽不是面向对象的语言,但可以模拟实现此原则。

 1 package main
 2 
 3 import "fmt"
 4 
 5 // ocp设计原则
 6 type Pet interface {
 7    eat()
 8 }
 9 type Dog struct{}
10 func (dog Dog) eat() {
11    fmt.Println(" dog eat")
12 }
13 
14 type Person struct{}
15 // 利用多态,设计java中的向上类型转换
16 func (per Person) care(p Pet) {
17    p.eat()
18 }
19 // 新增一个宠物,无需修改原有代码,只需做拓展即可
20 type Pig struct{}
21 func (pig Pig) eat() {
22    fmt.Println(" pig eat")
23 }
24 func main() {
25    dog := Dog{}
26    per := Person{}
27    per.care(dog)
28 
29    pig := Pig{}
30    per.care(pig)
31 }

 

标签:name,fmt,笔记,接口,func,Go,type,struct
From: https://www.cnblogs.com/RunningSnails/p/17375370.html

相关文章

  • Go笔记(十一):方法
    Go语言没有Java语言面向对象的特性,也无类对象的概念。但可以使用结构体实现这些特性。1、方法的声明Go中的方法是一种特殊的函数,与struct相关联,被称为struct的接收者。可以理解为方法就是有接收者的函数。语法格式如下:typemystructstruct{}func(recvmystruct)my......
  • CUDA入门笔记
    一个SM(StreamingMultiprocessor)中的所有SP(StreamingProcessor)是分成Warp的,共享同一个Memory和InstructionUnit(指令单元)。从硬件角度讲,一个GPU由多个SM组成(当然还有其他部分),一个SM包含有多个SP(以及还有寄存器资源,SharedMemory资源,L1cache,Scheduler,SPU,LD/ST单元等等)SM采......
  • selenium笔记之一些个人经验
    记录我避过坑之后,自认为好用的一些方法一、元素定位不再赘述有多少种类了虽然很多,但是我尝试使用用的最多最稳定的还是:.xpath这是我认为也是我用着最靠谱的一种方法二、点击我最开始看文档,用的是element.click()方法,但是发现,有些“变动”的元素或者一些其它可点击的标签不......
  • 【CacheLine】关于缓存行的笔记(存疑)
    什么是缓存行Cache是由很多个cacheline组成的。每个cacheline通常是64字节,并且它有效地引用主内存中的一块儿地址。一个Java的long类型变量是8字节,因此在一个缓存行中可以存8个long类型的变量。CPU每次从主存中拉取数据时,会把相邻的数据也存入同一个cacheline。在访问一......
  • 小D-新版接口自动化教程- http 请求 Requests 实战
     #-*-coding:UTF-8-*-importrequestsresponse=requests.get("https://www.baidu.com")print(response.text)......
  • 「学习笔记」AC 自动机
    「学习笔记」AC自动机点击查看目录目录「学习笔记」AC自动机算法问题思路代码例题KeywordsSearch玄武密码单词病毒最短母串文本生成器背单词密码禁忌前置:「学习笔记」字符串基础:Hash,KMP与Trie。好像对例题的讲解越来越抽象了?算法问题求\(n\)个单词在一个长度为\(......
  • string为接口的注意事项
    string为接口的注意事项问题描述​在一个应用程序中用到了另外一个库的dll,向dll的接口传递std::string参数时报错。由于这方面的问题比较多,所以我进行了深入研究。前置知识在vs项目右键->属性->C/C++->代码生成->运行库,有四个选项,/MD、/MDd、/MT、/MTd含有D的选项......
  • Python 脚本部署和发布 Django 应用程序的示例代码及注释
    代码说明:1、在脚本中定义了要部署的Django应用程序名称、Docker镜像名称和标签。2、使用字符串模板定义了KubernetesDeployment和Service的YAML文件。在字符串模板中使用了变量,用于替换实际的值。3、将Deployment和Service的YAML文件保存到本地文件中,并使用kube......
  • Jenkins 执行Docker build错误Got permission denied while trying to connect to the
    问题: 解决方法:这个报错为权限问题1.把jenkins用户,加到docker用户组 #如果没有docker用户组,先创建用户组:groupadddocker#添加jenkins用户到用户组:sudousermod-a-Gdockerjenkins-a<追加>必须与-G选项一起使用,把用户追加到某些组中。-G<群组>修改用户所属的......
  • collection接口
    Collection接口和常用方法collection接口包含:list和set两个接口而list里有Vector类,ArrayList类和LinkedList类set里有:Hashset类和Treeset类collection接口实现类的特点collection类实现子类可以存放多个元素,每个元素可以是Object有些Collection的实现类,可以存放重复的元素......