Golang学习 接口、多态性

多态特性所具备的基本要素:父类(接口)、子类(实现了父类的接口)、父类的变量(指针)引用子类的具体变量数据。

对外暴露的是父类(接口),最终实现功能的是子类。

package main

import "fmt"

// AnimalIF 本身是一个指针
type AnimalIF interface {
	GetColor() string
	GetType() string
	Sleep()
}

type Dog struct {
	color string
}

func (this *Dog) GetColor() string {
	return this.color
}
func (this *Dog) GetType() string {
	return "dog"
}
func (this *Dog) Sleep() {
	fmt.Println(this.GetType(), "is sleeping")
}

//至此,Dog类已经实现了 Animal接口的所有方法。

type Cat struct {
	color string
}

func (this *Cat) GetColor() string {
	return this.color
}
func (this *Cat) GetType() string {
	return "cat"
}
func (this *Cat) Sleep() {
	fmt.Println(this.GetType(), "is sleeping")
}

func ShowAnimal(animal AnimalIF) {
	animal.Sleep()
	fmt.Println("Animal's color is", animal.GetColor())
	fmt.Println("Animal's type is", animal.GetType())
}
func main() {
	var animal AnimalIF    //声明一个接口数据类型,他只是个指针
	animal = &Dog{"brown"} //为这个接口具体化
	animal.Sleep()
	fmt.Println("Animal's color is", animal.GetColor())
	fmt.Println("Animal's type is", animal.GetType())
	fmt.Println("===================")
	var animal2 AnimalIF = &Cat{"white"} //也可以声明的的时候直接实现接口
	animal2.Sleep()
	fmt.Println("Animal's color is", animal2.GetColor())
	fmt.Println("Animal's type is", animal2.GetType())
	fmt.Println("==========接下来将实现类的多态特性=========")
	ShowAnimal(&Dog{"yellow"})
	fmt.Println("===================")
	ShowAnimal(&Cat{"blank and white"})

}

输出结果如下:

dog is sleeping
Animal's color is brown
Animal's type is dog
===================
cat is sleeping
Animal's color is white
Animal's type is cat
==========接下来将实现类的多态特性=========
dog is sleeping
Animal's color is yellow
Animal's type is dog
===================
cat is sleeping
Animal's color is blank and white
Animal's type is cat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Golang中的接口是一种类型,它定义了一组方法的集合。接口提供了一种方式来描述对象的行为,而不关心对象的具体类型。通过接口,我们可以实现多和代码的灵活。 以下是一个简单的示例,演示了如何定义和使用接口: ```go package main import "fmt" // 定义一个接口 type Shape interface { Area() float64 Perimeter() float64 } // 定义一个矩形结构体 type Rectangle struct { width float64 height float64 } // 实现Shape接口的Area方法 func (r Rectangle) Area() float64 { return r.width * r.height } // 实现Shape接口的Perimeter方法 func (r Rectangle) Perimeter() float64 { return 2 * (r.width + r.height) } // 定义一个圆形结构体 type Circle struct { radius float64 } // 实现Shape接口的Area方法 func (c Circle) Area() float64 { return 3.14 * c.radius * c.radius } // 实现Shape接口的Perimeter方法 func (c Circle) Perimeter() float64 { return 2 * 3.14 * c.radius } func main() { // 创建一个矩形对象 rectangle := Rectangle{width: 5, height: 3} // 创建一个圆形对象 circle := Circle{radius: 4} // 使用接口类型的变量来调用方法 shapes := []Shape{rectangle, circle} for _, shape := range shapes { fmt.Println("Area:", shape.Area()) fmt.Println("Perimeter:", shape.Perimeter()) } } ``` 这个示例中,我们定义了一个`Shape`接口,它包含了`Area()`和`Perimeter()`两个方法。然后我们分别定义了`Rectangle`和`Circle`两个结构体,并实现了`Shape`接口的方法。在`main`函数中,我们创建了一个包含了矩形和圆形对象的切片,并使用接口类型的变量来调用方法。 运行以上代码,将会输出矩形和圆形的面积和周长。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值