golang语言渐入佳境[18]-interface接口

接口声明与定义

interface关键字,在接口中有函数,但是没有实现。

1
2
3
type Phone interface {
call()
}

例子

一旦有结构体实现了此函数,那么就可以用接口来接收此结构体。

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
package main

import "fmt"

type Phone interface {
call()
}

type AndroidPhone struct {
}

type IPhone struct {
}

func (a AndroidPhone) call() {
fmt.Println("我是安卓手机,可以打电话了")
}

func (i IPhone) call() {
fmt.Println("我是苹果手机,可以打电话了")
}

func main() {
// 定义接口类型的变量
var phone Phone
phone = new(AndroidPhone)
phone = AndroidPhone{}
fmt.Printf("%T , %v , %p \n" , phone , phone , &phone)
phone.call()

phone = new(IPhone)
phone = IPhone{}
fmt.Printf("%T , %v , %p \n" , phone , phone , &phone)
phone.call()
}

案例2

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main

import "fmt"

type Income interface {
calculate() float64 //计算收入总额
source() string     //用来说明收入来源
}

//固定账单项目
type FixedBilling struct {
projectName  string  //工程项目
biddedAmount float64 //项目招标总额
}

//定时生产项目(定时和材料项目)
type TimeAndMaterial struct {
projectName string
workHours   float64 //工作时长
hourlyRate  float64 //每小时工资率
}

//固定收入项目
func (f FixedBilling) calculate() float64 {
return f.biddedAmount
}

func (f FixedBilling) source() string {
return f.projectName
}

//定时收入项目
func (t TimeAndMaterial) calculate() float64 {
return t.workHours * t.hourlyRate
}

func (t TimeAndMaterial) source() string {
return t.projectName
}

//通过广告点击获得收入
type Advertisement struct {
adName         string
clickCount     int
incomePerclick float64
}

func (a Advertisement) calculate() float64 {
return float64(a.clickCount) * a.incomePerclick
}

func (a Advertisement) source() string {
return a.adName
}

func main() {
p1 := FixedBilling{"项目1", 5000}
p2 := FixedBilling{"项目2", 10000}
p3 := TimeAndMaterial{"项目3", 100, 40}
p4 := TimeAndMaterial{"项目4", 250, 20}
p5 := Advertisement{"广告1", 10000, 0.1}
p6 := Advertisement{"广告2", 20000, 0.05}

ic := []Income{p1, p2, p3, p4, p5, p6}
fmt.Println(calculateNetIncome(ic))
}

//计算净收入
func calculateNetIncome(ic []Income) float64 {
netincome := 0.0
for _, income := range ic {
fmt.Printf("收入来源:%s ,收入金额:%.2f \n", income.source(), income.calculate())
netincome += income.calculate()
}
return netincome
}

//说明:
// 没有对calculateNetIncome函数做任何更改,尽管添加了新的收入方式。全靠多态性而起作用。
// 由于新的Advertisement类型也实现了Income接口,可以将它添加到ic切片中。
// calculateNetIncome函数在没有任何更改的情况下工作,因为它可以调用Advertisement类型的calculate()和source()方法。

空接口

1
2
type A interface {
}

空接口可以接受任何的数据类型

1
2
3
4
5
6
7
type A interface {
}
var a1 A = Cat{"Mimi", 1}
var a2 A = Person{"Steven", "男"}
var a3 A = "Learn golang with me!"
var a4 A = 100
var a5 A = 3.14

定义map。value是任何数据类型

1
2
3
4
5
//2、定义map。value是任何数据类型
map1 := make(map[string]interface{})
map1["name"] = "Daniel"
map1["age"] = 13
map1["height"] = 1.71

定义一个切片,其中存储任意数据类型

1
2
3
slice1 := make([]interface{}, 0, 10)
slice1 = append(slice1, a1, a2, a3, a4, a5)
fmt.Println(slice1)

完整案例

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
package main

import (
"fmt"
)

type A interface {
}

type Cat struct {
name string
age  int
}

type Person struct {
name string
sex  string
}

func main() {
var a1 A = Cat{"Mimi", 1}
var a2 A = Person{"jonson", "男"}
var a3 A = "Learn golang with me!"
var a4 A = 100
var a5 A = 3.14

showInfo(a1)
showInfo(a2)
showInfo(a3)
showInfo(a4)
showInfo(a5)
fmt.Println("------------------")

//1、fmt.println参数就是空接口
fmt.Println("println的参数就是空接口,可以是任何数据类型", 100, 3.14, Cat{"旺旺", 2})

//2、定义map。value是任何数据类型
map1 := make(map[string]interface{})
map1["name"] = "Daniel"
map1["age"] = 13
map1["height"] = 1.71
fmt.Println(map1)
fmt.Println("------------------")

// 3、定义一个切片,其中存储任意数据类型
slice1 := make([]interface{}, 0, 10)
slice1 = append(slice1, a1, a2, a3, a4, a5)
fmt.Println(slice1)

}


func showInfo(a A) {
fmt.Printf("%T , %v \n", a, a)
}

接口对象转型的两种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//接口对象转型方式1
//instance,ok := 接口对象.(实际类型)
func getType(s Shape) {
if instance, ok := s.(Rectangle); ok {
fmt.Printf("矩形:长度%.2f , 宽度%.2f , ", instance.a, instance.b)
} else if instance, ok := s.(Triangle); ok {
fmt.Printf("三角形:三边分别:%.2f , %.2f , %.2f , ", instance.a, instance.b, instance.c)
} else if instance, ok := s.(Circle); ok {
fmt.Printf("圆形:半径%.2f , ", instance.radius)
}
}

//接口对象转型——方式2
//接口对象.(type),  配合switch和case语句使用
func getType2(s Shape) {
switch instance := s.(type) {
case Rectangle:
fmt.Printf("矩形:长度为%.2f , 宽为%.2f ,\t", instance.a, instance.b)
case Triangle:
fmt.Printf("三角形:三边分别为%.2f ,%.2f , %.2f ,\t", instance.a, instance.b, instance.c)
case Circle:
fmt.Printf("圆形:半径为%.2f ,\t", instance.radius)
}
}

接口对象转型案例 求周长或者面积

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main

import (
"math"
"fmt"
)

//1、定义接口
type Shape interface {
perimeter() float64
area() float64
}

//2.矩形
type Rectangle struct {
a, b float64
}

//3.三角形
type Triangle struct {
a, b, c float64
}

//4.圆形
type Circle struct {
radius float64
}

//定义实现接口的方法
func (r Rectangle) perimeter() float64 {
return (r.a + r.b) * 2
}

func (r Rectangle) area() float64 {
return r.a * r.b
}

func (t Triangle) perimeter() float64 {
return t.a + t.b + t.c
}

func (t Triangle) area() float64 {
//海伦公式
p := t.perimeter() / 2 //半周长
return math.Sqrt(p * (p - t.a) * (p - t.b) * (p - t.c))
}

func (c Circle) perimeter() float64 {
return 2 * math.Pi * c.radius
}

func (c Circle) area() float64 {
return math.Pow(c.radius, 2) * math.Pi
}

//接口对象转型方式1
//instance,ok := 接口对象.(实际类型)
func getType(s Shape) {
if instance, ok := s.(Rectangle); ok {
fmt.Printf("矩形:长度%.2f , 宽度%.2f , ", instance.a, instance.b)
} else if instance, ok := s.(Triangle); ok {
fmt.Printf("三角形:三边分别:%.2f , %.2f , %.2f , ", instance.a, instance.b, instance.c)
} else if instance, ok := s.(Circle); ok {
fmt.Printf("圆形:半径%.2f , ", instance.radius)
}
}

//接口对象转型——方式2
//接口对象.(type),  配合switch和case语句使用
func getType2(s Shape) {
switch instance := s.(type) {
case Rectangle:
fmt.Printf("矩形:长度为%.2f , 宽为%.2f ,\t", instance.a, instance.b)
case Triangle:
fmt.Printf("三角形:三边分别为%.2f ,%.2f , %.2f ,\t", instance.a, instance.b, instance.c)
case Circle:
fmt.Printf("圆形:半径为%.2f ,\t", instance.radius)
}
}

func getResult(s Shape) {
getType2(s)
fmt.Printf("周长:%.2f ,面积:%.2f \n", s.perimeter(), s.area())
}

func main() {
var s Shape
s = Rectangle{3, 4}
getResult(s)
showInfo(s)

s = Triangle{3, 4, 5}
getResult(s)
showInfo(s)

s = Circle{1}
getResult(s)
showInfo(s)

x := Triangle{3, 4, 5}
fmt.Println(x)

}

func (t Triangle) String() string {
return fmt.Sprintf("Triangle对象,属性分别为:%.2f, %.2f, %.2f", t.a, t.b, t.c)
}

func showInfo(s Shape) {
fmt.Printf("%T ,%v \n", s, s)
fmt.Println("-------------------")
}

image.png

猜你喜欢

转载自blog.51cto.com/13784902/2326696
今日推荐