Go中interface嵌入和interface转换

栏目: Go · 发布时间: 8年前

// 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()

}

//总结:通过赋值或者参数传递的形式,完成接口的调用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

An Introduction to Probability Theory and Its Applications

An Introduction to Probability Theory and Its Applications

William Feller / Wiley / 1991-1-1 / USD 120.00

Major changes in this edition include the substitution of probabilistic arguments for combinatorial artifices, and the addition of new sections on branching processes, Markov chains, and the De Moivre......一起来看看 《An Introduction to Probability Theory and Its Applications》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具