您好,登錄后才能下訂單哦!
// 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()
}
//總結(jié):通過賦值或者參數(shù)傳遞的形式,完成接口的調(diào)用。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。