第 十八 课 接口

把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。

package main

import "fmt"

type Phone interface {
     call()
}

type NokiaPhone struct {

}

func (nokia NokiaPhone) call() {
    fmt.Println("I am Nokia")
}

func main() {
    var phone Phone
    phone = new(NokiaPhone)
    phone.call()
}

 

解golang的interface主要在于以下两点:

  • interface是方法的集合
  • interface是一种类型
posted @ 2018-06-18 12:03  刘大飞  阅读(118)  评论(0编辑  收藏  举报