GoLang学习笔记(三十六)接口对象的转型

接口对象转型
方式一:
instance,ok:=接口对象.(实际类型)
如果该接口对象是对应的实际类型,那么instance就是转型之后对象,ok的值为true
配合if...else if...使用
方式二:
接口对象.(type)
配合switch...case语句使用

type shape interface {
	perimeter() float64
	area() float64
}

type rectangle struct {
	a, b float64
}

type triangle struct {
	a, b, c float64
}

type circular struct {
	radius float64
}

func (r rectangle) perimeter() float64 {
	return (r.a + r.b) * 2
}
func (r rectangle) area() float64 {
	return r.a * r.b
}

func (t triangle) perimeter() float64 {
	return t.a + t.b + t.c
}
func (t triangle) area() float64 {
	p := t.perimeter() / 2
	return math.Sqrt(p * (p - t.a) * (p - t.b) * (p - t.c))
}
func (c circular) perimeter() float64 {
	return c.radius * math.Pi * 2
}
func (c circular) area() float64 {
	return math.Pow(c.radius, 2) * math.Pi
}

func getType(s shape) {
    if i,ok := s.(rectangle); ok{
		fmt.Printf("图形的长:%.2f,图形的宽:%.2f \n", i.a, i.b)
	} else if i,ok := s.(triangle); ok {
		fmt.Printf("图形的第一个边:%.2f,图形的第二个边:%.2f,图形的第三个边:%.2f \n", i.a, i.b, i.c)
	} else if i,ok := s.(circular); ok {
		fmt.Printf("图形的半径:%.2f \n",i.radius)
	}
}
func getResult(s shape) {
	fmt.Printf("周长为:%.2f,面积为:%.2f \n",s.perimeter(),s.area())
	fmt.Println("------------------------------")
}

func transInterface1() {
	var s shape

	s = rectangle{10, 20}
	getType(s)
	getResult(s)

	s = triangle{30, 40, 50}
	getType(s)
	getResult(s)

	s = circular{50}
	getType(s)
	getResult(s)

}

上面的例子使用的是方式一,如果要使用方式2,可以将getType()函数改为:

func getType(s shape) {
	switch i := s.(type) {
	case rectangle:
		fmt.Printf("图形的长:%.2f,图形的宽:%.2f \n", i.a, i.b)
	case triangle:
		fmt.Printf("图形的第一个边:%.2f,图形的第二个边:%.2f,图形的第三个边:%.2f \n",i.a,i.b,i.c)
	case circular:
		fmt.Printf("图形的半径:%.2f \n",i.radius)
	}
}

PS:上面求三角形面积使用了海伦公式求三角形的面积,公式为:

三角形的面积=平方根[三角形周长的一半×(三角形周长的一半减去第一个边)×(三角形周长的一半减去第二个边)×(三角形周长的一半减去第三个边)]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值