接口声明与定义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() }
|
案例21 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 }
|
空接口空接口可以接受任何的数据类型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("------------------")
fmt.Println("println的参数就是空接口,可以是任何数据类型", 100, 3.14, Cat{"旺旺", 2})
map1 := make(map[string]interface{}) map1["name"] = "Daniel" map1["age"] = 13 map1["height"] = 1.71 fmt.Println(map1) fmt.Println("------------------")
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
|
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) } }
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" )
type Shape interface { perimeter() float64 area() float64 }
type Rectangle struct { a, b float64 }
type Triangle struct { a, b, c float64 }
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 }
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) } }
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("-------------------") }
|
本文链接: https://dreamerjonson.com/2018/11/27/golang-18-interface/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

|