代码如下:
package main
import "fmt"
type Hummaner interface {
sing() //声明一个方法,并没有实现
}
type Studenter interface {
Hummaner //匿名字段,嵌入字段
play() //声明一个方法,并没有实现
}
type Student struct {
id int
name string
}
//type Techer struct {
// id int
// name string
//}
func (s *Student) sing() {
fmt.Println("学生在唱歌!!!")
}
func (t *Student) play() {
fmt.Println("老师在玩耍!!!")
}
func main() {
//studenter是超集,hummer是子集
var s Studenter = &Student{1, "steven"} //指针接收
var h Hummaner = s //超集可以转子集
h.sing()
var h1 Hummaner = &Student{1, "steven"}
//var s1 Studenter = h1// cannot use h1 (type Hummaner) as type Studenter in assignment
//s1.sing()
}
原文:https://www.cnblogs.com/zhaopp/p/11565515.html