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 {
addr string
group string
}
func (tmp *Teacher) sayhi() {
fmt.Printf("Teacher[%s, %s] sayhi\n", tmp.addr, tmp.group)
}
type Mystr string
func (tmp *Mystr) sayhi() {
fmt.Printf("MyStr[%s] sayhi\n", *tmp)
}
func whoSayHi(i Humaner) {
i.sayhi()
}
func main() {
s := &Student{"mike", 666}
t := &Teacher{"bj", "go"}
var str Mystr = "hello mike"
whoSayHi(s)
whoSayHi(t)
whoSayHi(&str)
x := make([]Humaner, 3)
x[0] = s
x[1] = t
x[2] = &str
for _, i := range x {
i.sayhi()
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結