[Go实战]interface用法

167 阅读1分钟
package main

import (
	"fmt"
)

func main() {
	NewWork(true).do()
	NewWork(false).do()
}

type Work interface {
	do()
}

func NewWork(isEmployee bool) Work{
	if isEmployee{
		return &Employee{}
	}
	return &Employer{}
}

type Employee struct {}

func (p *Employee) do() () {
	fmt.Println(`我是雇员`)
}

type Employer struct {}

func (p *Employer) do() () {
	fmt.Println(`我是老板`)
}