Learn Golang in Days - Day 14
簡介
- Go語言提供了另外一種數(shù)據(jù)類型就是接口庶骄,它把所有具有共性的方法定義在一起,只要實現(xiàn)了這些方法就是實現(xiàn)了這個接口践磅。
package main
import "fmt"
/* 聲明接口 */
type Phone interface {
call()
}
/* 定義結(jié)構(gòu)體 */
type NokiaPhone struct {
}
/* 實現(xiàn)接口方法 */
func (nokiaPhone NokiaPhone) call() {
fmt.Printf("I am nokia phone.\n")
}
/* 定義結(jié)構(gòu)體 */
type IPhone struct {
}
/* 實現(xiàn)接口方法 */
func (iPhone IPhone) call() {
fmt.Printf("I am iphone.\n")
}
func main() {
var phone Phone
phone = new(NokiaPhone)
phone.call()
phone = new(IPhone)
phone.call()
}