go语言 多态

package main

import "fmt"


type Humaner interface {
	Say()
}

type Student struct {
	name  string
	age   int
}

type Teacher struct {
	name    string
	age     int
}

func (s *Student) Say() {
	fmt.Printf(s.name, s.age)
}

func (t *Teacher) Say() {
	fmt.Printf(t.name, t.age)
}

//多态的实现
//将接口作为函数参数  实现多态
func SayHi(h Humaner){
	h.Say()
}

func main() {

	stu:=Student1{"wang",18}
	//调用多态函数
	SayHi(&stu)
}

你可能感兴趣的:(go)