Go中interface嵌入和interface轉換

來源:互聯網
上載者:User

標籤:總結   ace   int   project   形式   var   interface   ack   pac   

// code_22_interface_field_interface project main.gopackage mainimport ( "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()}//總結:通過賦值或者參數傳遞的形式,完成介面的調用。

Go中interface嵌入和interface轉換

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.