在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
package main import "fmt" type Humaner interface { SayHi() } type Student struct { name string id int } func (tmp *Student) SayHi() { fmt.Printf("Student%s %d sayhi\n", tmp.name, tmp.id) } type Teacher struct { group string addr string } func (tmp *Teacher) SayHi() { fmt.Printf("Teacher%s %s sayhi\n", tmp.group, tmp.addr) } type MyStr string func (tmp *MyStr) SayHi() { fmt.Printf("MyStr %s sayhi", *tmp) } func main() { //定义接口类型的变量 var i Humaner //只是实现了此接口方法的类型,那么这个类型的变量(接收者类型)就可以给i赋值 s := &Student{"mike", 1} i = s i.SayHi() t := &Teacher{"mike", "NJ"} i = t i.SayHi() var str MyStr = "hello mike" i = &str i.SayHi() } 执行结果 Studentmike 1 sayhi Teachermike NJ sayhi MyStr hello mike sayhi
|
请发表评论