您的位置:首页 > 编程语言 > Go语言

go struct的嵌套/组合以及interface

2018-11-16 19:39 337 查看
版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/84136563

      看代码:

[code]package main
import "fmt"

type Intf interface {
process()
}

type MsgBase struct {
id int
}

func (p *MsgBase) process() {
fmt.Printf("base %v\n", p)
}

type Msg1 struct {
MsgBase
x int
}

type Msg2 struct {
MsgBase
x int
y int
}

func (p *Msg1) process() {
fmt.Printf("business %v\n", p)
}

func main() {
var m Intf = new(Msg1)   // 不能用var m MsgBase = new(Msg1)
m.process()

m = new(Msg2)
m.process()
}

         结果:

business &{{0} 0}
base &{0}

  

        不多收。

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: