go 接口实现

package main

import (
	"fmt"
)

// 定义接口
type Beahavior interface {
	Run() string
	Eat(thing string) string
}

type Dog struct {
	Name string
}

func (d * Dog) Eat(thing string ) string   {
	return "hello i am eating "+thing
}

func (d * Dog) Run() string   {
	return "hello i am running"
}


func main()  {

	d := Dog{Name:"旺财"}
	res := d.Eat("apple")
	fmt.Println(res)
}

  

结构体要定义了接口的所有方法才算是类型实现了该interface

posted @ 2020-06-07 07:58  brady-wang  阅读(527)  评论(0编辑  收藏  举报