接口定义了一组方法(方法集),但是这些方法不包含(实现)代码—它们没有被实现(它们是抽象的),接口里也不能包含变量。
package main
import "fmt"
// 创建接口
// 创建结构体
// 将结构体绑定到接口上
type ss interface {
call()
}
type phone struct {
tel int
}
type ipad struct {
id int
}
func (p *phone) call() {
p.tel = 123456
fmt.Println("hell word")
fmt.Println(p.tel)
}
func (i *ipad) call() {
i.id = 456789
fmt.Println(i.id)
}
func main() {
var s ss
s = new(phone)
// s := new(phone)
s.call()
s = new(ipad)
s.call()
}
}
//hell word
//123456
//456789