看代码:

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}

  

        不多说。

 

 


本文转载:CSDN博客