go语言学习笔记九 接口基本使用和最佳实践

1.基本使用 接口是引用类型

package main

import "fmt"

// 定义接口
type Usb interface {
	// 声明两个没有实现的方法
	// 在其他结构体中使用时必须实现所有方法
	Start(a int) int
	Stop()
}

// 定义 3个结构体
type Phone struct {
}

type Camera struct {
}

type Computer struct {
}

// 实现方法
func (p Phone) Start(a int) int {
	fmt.Printf("1.Phone Start 方法:%v\n", a)
	return a + 1
}

func (p Phone) Stop() {
	fmt.Printf("2.Phone Stop 方法\n")
}

func (c Camera) Start(a int) int {
	fmt.Printf("1.Camera Start 方法:%v\n", a)
	return a + 1
}

func (c Camera) Stop() {
	fmt.Printf("2.Camera Stop 方法\n")
}

// 定义使用接口的函数 usb 为实现结构方法的实例
func (c Computer) Working(usb Usb, a int) (res int) {
	res = usb.Start(a)
	usb.Stop()
	return res
}

func main() {
	p := Phone{}
	ca := Camera{}
	c1 := Computer{}
	// usb 为实现Usb接口方法的实例
	res := c1.Working(p, 20)
	fmt.Printf("3. Working返回数据%v\n", res)
	res2 := c1.Working(ca, 50)
	fmt.Printf("4. Working返回数据%v\n", res2)
	var p2 Phone
	// 可以定义一个变量 类型是接口 = 实现接口方法的结构体的实例
	var a Usb = p2
	a.Start(88)
	a.Stop()
}

输出

1.Phone Start 方法:20
2.Phone Stop 方法
3. Working返回数据21
1.Camera Start 方法:50
2.Camera Stop 方法
4. Working返回数据51
1.Phone Start 方法:88
2.Phone Stop 方法


2 最佳实践

package main

import (
    "fmt"
    "math/rand"
	"sort"
)

// 定义学生结构体
type Student struct {
	Name  string
	Age   int
	Score float64
}

// 定义学生结构体的切片
type StudentSlice []Student

// studentSlice 实现内置排序接口 sort.Sort 的方法
//Len() int; Less(i, j int) bool; Swap(i, j int); Swap(i, j int)
func (stu StudentSlice) Len() int {
	return len(stu)
}

func (stu StudentSlice) Less(i, j int) bool {
	return stu[i].Score < stu[j].Score
}

func (stu StudentSlice) Swap(i, j int) {
	stu[i], stu[j] = stu[j], stu[i]
}

func main() {
	// 创建 StudentSlice 的实例
	var studentSlice StudentSlice
	// 在s1 中增加学生信息
	for i := 0; i < 10; i++ {
		stu := Student{
			Name: fmt.Sprintf("名字%d", rand.Intn(100)),
			Age : rand.Intn(100),
			Score: float64(rand.Intn(100)),
		}
		studentSlice = append(studentSlice, stu)
	}
	fmt.Printf("--------学生信息--------\n")
	for index, stu :=  range studentSlice{
		fmt.Printf("%v.Name:%v\n", index, stu.Name)
		fmt.Printf("%v.Age:%v\n", index, stu.Age)
		fmt.Printf("%v.Score:%v\n\n", index, stu.Score)
	}

	fmt.Printf("--------根据分数排序后学生信息--------\n")
	sort.Sort(studentSlice)
	for index, stu := range studentSlice {
		fmt.Printf("%v.Name:%v\n", index, stu.Name)
		fmt.Printf("%v.Age:%v\n", index, stu.Age)
		fmt.Printf("%v.Score:%v\n\n", index, stu.Score)
	}
}

输出

--------学生信息--------
0.Name:名字81
0.Age:87
0.Score:47

1.Name:名字59
1.Age:81
1.Score:18

2.Name:名字25
2.Age:40
2.Score:56

3.Name:名字0
3.Age:94
3.Score:11

4.Name:名字62
4.Age:89
4.Score:28

5.Name:名字74
5.Age:11
5.Score:45

6.Name:名字37
6.Age:6
6.Score:95

7.Name:名字66
7.Age:28
7.Score:58

8.Name:名字47
8.Age:47
8.Score:87

9.Name:名字88
9.Age:90
9.Score:15

--------根据分数排序后学生信息--------
0.Name:名字0
0.Age:94
0.Score:11

1.Name:名字88
1.Age:90
1.Score:15

2.Name:名字59
2.Age:81
2.Score:18

3.Name:名字62
3.Age:89
3.Score:28

4.Name:名字74
4.Age:11
4.Score:45

5.Name:名字81
5.Age:87
5.Score:47

6.Name:名字25
6.Age:40
6.Score:56

7.Name:名字66
7.Age:28
7.Score:58

8.Name:名字47
8.Age:47
8.Score:87

9.Name:名字37
9.Age:6
9.Score:95


猜你喜欢

转载自blog.csdn.net/qq_38165374/article/details/105356797
今日推荐