2022-5-14 实习Day36

1、行内汇款——申报交易完成在监管报送平台查询不到问题排查。 –3小时 30%
2、代理行业务测试 –2小时 50%
3、代理行业务测试102报文前端校验不通过 –2小时 30%

Golang编程学习(part 31)

1、面向对象编程 — 多重继承

如果一个struct嵌套了多个匿名结构体,那么该结构体可以直接访问嵌套的匿名结构体的字段和方法,从而实现了多重继承

1
2
3
4
5
6
7
8
9
10
11
12
type Goods struct {
Name string
Price float64
}
type Brand struct {
Name string
Address string
}
type TV struct {
Goods
Brand
}

1)如果嵌入的匿名结构体有相同的字段名或者方法名,则在访问时需要通过匿名结构体类型名来区分

2)为了保证代码的简洁性,建议大家尽量不使用多重继承

2、接口(interface)

基本介绍:按顺序,我们应该讲解多态,但是在讲解多态前,我们需要讲解接口(interface),因为在Golang中多态特性主要是通过接口来实现的

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
// usb插槽就是现实中的接口
// 你可以把手机、相机、u盘都插在usb插槽上,而不用担心哪个插槽是专门插哪个的,原因是做usb插槽
// 的厂家和做各种设备的厂家都遵守了统一的规定,包括尺寸、排线等

package main
import (
"fmt"
)

// 声明一个接口
type Usb interface {
// 声明两个没有实现的方法
Stopp()
Start()
}

type Phone struct {}

// 让Phone实现Usb接口的方法
func (p Phone) Stopp() {
fmt.Println("手机停止工作...")
}
func (p Phone) Start() {
fmt.Println("手机开始工作...")
}

type Camera struct {}

// 让Camera实现Usb接口的方法
func (c Camera) Stopp() {
fmt.Println("相机停止工作...")
}
func (c Camera) Start() {
fmt.Println("相机开始工作...")
}

// 计算机
type Computer struct {}

// 编写一个Working方法,接收一个Usb接口类型变量
// 只要是实现了Usb接口(所谓实现了Usb接口,就是指实现了Usb接口声明的所有方法)
// usb变量会根据传入的实参来判断到底是Phone还是Camera
func (computer Computer) Working(usb Usb) {
usb.Start()
usb.Stopp()
}
func main() {
var computer Computer
var phone Phone
var camera Camera
computer.Working(phone)
computer.Working(camera)
}

手机开始工作...
手机停止工作...
相机开始工作...
相机停止工作...

① 接口概念再说明及基本语法
interface类型可以定义一组方法,但是这些不需要实现。并且interface不能包含任何变量。到某个自定义类型(比如结构体 Phone)要使用的时候,在根据具体情况把这些方法写出来(实现)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 基本语法
type 接口名 interface {
method1(参数列表) 返回值列表
method2(参数列表) 返回值列表
}
|
|
\|/
func(t 自定义类型) method1(参数列表) 返回值列表 {
// 方法实现
}
func(t 自定义类型) method2(参数列表) 返回值列表 {
// 方法实现
}

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

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

② 接口使用的细节

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

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import "fmt"
type MyInter interface {
Say()
}

type interger int
func (i interger) Say() {
fmt.Println("i=", i)
}

func main() {
var i interger = 10
var b MyInter = i
b.Say()
}

i= 10

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
type AInterface interface {
Say()
}
type BInterface interface {
Hello()
}

type Monster struct {}

func (m Monster) Say() {
fmt.Println("Monster Say()")
}
func (m Monster) Hello() {
fmt.Println("Monster Hello()")
}

func main() {
// Monster实现了AInterface和BInterface
var monster Monster
var a AInterface = monster
var b BInterface = monster
a.Say()
b.Hello()
}

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

1
2
3
4
type AInterface interface {
Say()
// Name string
}

8)一个接口(比如A接口)可以继承多个别的接口(比如B、C接口),这时如果要实现A接口,也必须将B、C接口的方法全部实现

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
package main
import "fmt"
type A interface {
test1()
}
type B interface {
test2()
}
type C interface {
A
B
test3()
}

// 如果需要实现C接口,就需要将B、C接口的方法都实现
type Student struct {}

func (s Student) test1() {
fmt.Println("test1")
}
func (s Student) test2() {
fmt.Println("test2")
}
func (s Student) test3() {
fmt.Println("test3")
}
func main() {
var stu Student
var c C = stu
c.test1()
c.test2()
c.test3()
}

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main
import (
"fmt"
"reflect"
)

type T interface {}

type Student struct {
Name string
Age int
}

func main() {
student := Student{"张三", 25}
var t T = student
fmt.Println(t, reflect.TypeOf(t)) // {张三 25} main.Student
var tt interface{} = 5
fmt.Println(tt, reflect.TypeOf(tt)) // 5 int
}
③ 课堂练习
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
// 下面代码的书写完全是没问题的
package main
import "fmt"
type AInterface interface {
Test1()
Test2()
}
type BInterface interface {
Test1()
Test3()
}

type Student struct {}

func (s Student) Test1() {}
func (s Student) Test2() {}
func (s Student) Test3() {}

func main() {
student := Student{}
var a AInterface = student
var b BInterface = student
fmt.Println(a, b)
}

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
// 下面代码没问题
package main
import "fmt"
type AInterface interface {
Test1()
Test2()
}
type BInterface interface {
Test1()
Test3()
}

type CInterface interface {
AInterface
BInterface
}

type Student struct {}

func (s Student) Test1() {
fmt.Println("test1")
}
func (s Student) Test2() {}
func (s Student) Test3() {}

func main() {
student := Student{}
student.Test1() // test1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main
import "fmt"
type Usb interface {
Say()
}

type Student struct {}
func (s *Student) Say() {
fmt.Println("say()")
}

func main() {
var stu Student = Student{}

// 错误!
// Cannot use 'stu' (type Student)
// as the type Usb Type does not implement 'Usb'
// as the 'Say' method has a pointer receiver
// var u Usb=stu

var u Usb = &stu
u.Say() //say()
}