Go中结构体和接口的定义

package main
import (
   "fmt"
)

//定义接口
type woman interface {//定义一个女人的接口,定义一个爱的方法
   love()
   makelove()
}
//定义一个结构体
type teacher struct {

   name string
   Age int
}
//实现接口
func (p *teacher)  love() {
   fmt.Println(p.Age,"gan")
}
//type human interface {
// //只有声明没有实现,也没有类型
// eat()
//}
//
//type Student struct {
// name string
//}
//
实现接口方法
//func (s *Student) eat() {
// fmt.Println(s.name + " eat")
//}

func main() {
   //s := Student{"yy"}
   tt :=teacher{"Alice",18}
   //(&s).eat()
   (&tt).love()
}

猜你喜欢

转载自blog.csdn.net/zhuiyunzhugang/article/details/109586552