GOLANG学习27. 接口定义
package main
import (
"fmt"
)
/**
*定义人的接口
*/
type Ipeople interface {
Eat()
Sleep()
}
//定义学生类
type Student struct {
Name string
Age int
Grade int
}
//定义医生类
type Dotocr struct {
Name string
Age int
Occupation string
}
//学生结构体的方法
func (s Student) Eat() {
fmt.Printf("%s is the %d grade,they eat very nice\n", s.Name, s.Grade)
}
func (s Student) Sleep() {
fmt.Printf("%s is %d years old,he sleep very nice\n", s.Name, s.Age)
}
//医生结构体的方法
func (d Dotocr) Eat() {
fmt.Printf("%s Occupation is %s\n", d.Name, d.Occupation)
}
func (d Dotocr) Sleep() {
fmt.Printf("%s is %d years old,he sleep very nice\n", d.Name, d.Age)
}
func main() {
var ip Ipeople
var stu Student = Student{
Name: "张三",
Age: 12,
Grade: 5,
}
ip = stu
ip.Eat()
ip.Sleep()
var dot Dotocr = Dotocr{
Name: "李四",
Age: 30,
Occupation: "dotocr",
}
ip = dot
ip.Eat()
ip.Sleep()
}