首页 > 编程语言 > 详细

GO语言面向对象04---接口的继承

时间:2019-11-22 01:11:57      阅读:96      评论:0      收藏:0      [点我收藏+]
package main

import "fmt"

type Animal interface {
	Eat(food string) (shit string)
	GoDie()
}

type Fighter interface {
	Attack() (bloodloss int)
	Defend()
}

type Beast interface {
	//显示的继承Animal接口
	Animal

	//隐式的继承Fighter接口
	Attack() (bloodloss int)
	Defend()

	//独有的抽象方法
	Run()
}

type Tiger struct {
	Name string
	Food string
	Shit string
}

func (t *Tiger)Eat(food string) (shit string){
	fmt.Printf("本王%s正在吃%s\n",t.Name,t.Food)
	return "虎翔"
}
func (t *Tiger)GoDie(){
	fmt.Printf("本王%s驾鹤西归\n",t.Name)
}
func (t *Tiger)Attack() (bloodloss int){
	fmt.Printf("本王%s发起攻击\n",t.Name)
	return 100
}
func (t *Tiger)Defend(){
	fmt.Printf("本王%s躺地上举起爪子,休想伤害本喵\n",t.Name)
}
func (t *Tiger)Run(){
	fmt.Printf("本王%s在大地上奔跑\n",t.Name)
}

func main() {
	var animal Animal
	var fighter Fighter
	var beast Beast

	tiger := &Tiger{"辛巴", "一切会走动的活物", "虎翔"}
	
	animal = tiger
	fighter = tiger
	beast = tiger

	animal.GoDie()
	fighter.Attack()
	beast.Run()
}

结果:

本王辛巴驾鹤西归
本王辛巴发起攻击
本王辛巴在大地上奔跑

  

GO语言面向对象04---接口的继承

原文:https://www.cnblogs.com/yunweiqiang/p/11909282.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!