[GO]接口的转换

package main

import "fmt"

type Humaner interface { //子集
    SayHi()
}

type Personer interface { //超集
    Humaner //匿名字段
    Sing(lrc string)
}

type Student struct {
    name string
    id int
}

func (s *Student)SayHi()  {
    fmt.Printf("%s sayhi
", s.name)
}

func (p *Student)Sing(lrc string)  {
    fmt.Printf("student %s sing %s
", p.name, lrc)
}

func main() {
    //超集可以转换为子集,即可以对子集赋值,就是大的可以给小的,但是反过来就不可以
    var ipro Personer
    ipro = &Student{}
    var i Humaner
     i = ipro
     //但是ipro=i,这样就是不可以的
     i.SayHi()
}

执行结果为sayhi

原文地址:https://www.cnblogs.com/baylorqu/p/9642765.html