Learn Golang in Days - Day 14
摘要:
Learn Golang in Days - Day 14
簡介
Go語言提供了另外一種資料型別就是介面,它把所有具有共性的方法定義在一起,只要實現了這些方法就是實現了這個介面。
package main
import "fmt"
/* 宣告介面...
Learn Golang in Days - Day 14
簡介
- Go語言提供了另外一種資料型別就是介面,它把所有具有共性的方法定義在一起,只要實現了這些方法就是實現了這個介面。
package main import "fmt" /* 宣告介面 */ type Phone interface { call() } /* 定義結構體 */ type NokiaPhone struct { } /* 實現介面方法 */ func (nokiaPhone NokiaPhone) call() { fmt.Printf("I am nokia phone.\n") } /* 定義結構體 */ type IPhone struct { } /* 實現介面方法 */ 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() }