// code_22_interface_field_interface project main.go
package main
import (
"fmt"
)
type Humaner interface {
SayHi()
}
type Personer interface {
Humaner
Sing(lyrics string) //lyrics 歌词
}
type Student struct {
name string
score float64
}
func (s *Student) SayHi() {
fmt.Printf("Student[%s, %f]say hi!\n", s.name, s.score)
}
func (s *Student) Sing(lyrics string) {
fmt.Printf("Student sing[%s]!!\n", lyrics)
}
func main() {
s := &Student{"ck_god", 88.88}
var i2 Personer
i2 = s //接口指向struct对象
i2.SayHi()
i2.Sing("god is a girl")
//var i3 Humaner = &Student{"ck_god", 88.88}
//var i4 Personer = i1 //err
//Personer为超集,Humaner为子集
var i5 Personer = &Student{"ck_god", 88.88}
var i6 Humaner = i5
i6.SayHi()
}
//总结:通过赋值或者参数传递的形式,完成接口的调用。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。