GO语言的多态

86 阅读1分钟

首先GO语言的多态,在不同结构体中,同一个函数名称有着不同的功能

type Student struct {
	name string
	age  int
	sex  int
	id   string
}
type admin struct {
	name string
	age  int
}

func (this *Student) index() *Student {
	return this
}
func (this *admin) getname() {//声明在admin中结构体的方法
	fmt.Println("the admin name is ", this.name)
}
func (this *Student) getname() {//声明在student结构体中的方法,方法命与admin相同,但是函数内容不同,函数的多态
	fmt.Println("The student name is ", this.name)
}