标签:alt 声明 inf int imm type height pack key
package main import ( "fmt" ) //猴子结构体 type Monkey struct { Name string } func (this *Monkey) climbing() { fmt.Println(this.Name, " 生来会爬树..") } //声明接口 type BirdAble interface { Flying() } type FishAble interface { Swimming() } //小猴子结构体 type LittleMonkey struct { Monkey //继承猴子 } //实现接口-->在不破坏继承的关系的基础上对结构体进行功能的扩展 //让小猴子实现Flying() func (this *LittleMonkey) Flying() { fmt.Println(this.Name, " 通过学习,会飞翔...") } //让小猴子实现Swimming() func (this *LittleMonkey) Swimming() { fmt.Println(this.Name, " 通过学习,会游泳..") } func main() { //创建一个小猴子实例 monkey := LittleMonkey{ Monkey{ Name: "悟空", }, } monkey.climbing() monkey.Flying() monkey.Swimming() }
标签:alt 声明 inf int imm type height pack key
原文地址:https://www.cnblogs.com/yzg-14/p/12233696.html