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

原文地址:https://www.cnblogs.com/php-linux/p/13058933.html