Go语言基础接口类型

 go语言是提供了一个类型叫接口类型的来定义接口
1、Go语言的接口它是隐式实现。也就是说对于一个具体的类型,无须声明它实现了哪些接口,只要该类型提供了接口所必须的方法即可。
 这种设计让你无须改变已有类型的实现,就可以为这些类型扩展新的接口。
 2、一个接口类型定义了一组方法,如果用一个具体的类型例如struct类型来实现该接口,那么struct就必须实现该接口类型定义的所有方法,否则就该struct就没有实现该接口。
 3、一个接口可以被多个类型例如struct、func等实现,一个类型也可以实现多个接口。
 

/****************************************************************************
* 下面是实现接口示例:
* 本示例中
* 1、声明了接口类型Shape,
* 2、声明了struct类型 Circle、Square以及Rectangle;
* Circle类型实现了shape接口的所有方法,因此说Circle实现了接口Shape;
* Square类型实现了shape接口的所有方法,因此说Square实现了接口Shape;
* Triangle类型只实现了接口的一部分方法,因此Triangle未实现接口Shape。
****************************************************************************/

package main

import (
  "fmt"
)

///////////////////////////////////////////////////////////////////////
//接口是抽象的,所以很灵活的,只定义方法,具体实现由各自来实现。
/*
Shape是形状的抽象
1、type Circle struct {} //实现圆
2、type Square struct {} //实现四方形
都有共性形状,但是各自又有各自特性
*/
type Shape interface {
  GetArea() float64
  GetPerimeter() float64
}

///////////////////////////////////////////////////////////////////////
//1、圆结构体,因为圆本身特性,所以具有圆周率
type Circle struct {
  R float64
}

/*************************************************************************************
 这里还有几个知识点
1、为某个类型增加方法 请看这里
2、接收器概念 请看这里
**************************************************************************************/

//Circle 实现 Shape 接口的 GetArea 方法 ,返回float64类型数据
func (c Circle)GetArea() float64 {
  return 3.14 * c.R
}

// Circle 实现 Shape 接口的 GetPerimeter 方法
func (c Circle)GetPerimeter() float64 {
  return 3.14 * 2 * c.R
}


///////////////////////////////////////////////////////////////////////
//2、四方形结构体,最典型特征就是宽和高
type Square struct {
  Width float64
  High float64
}
//Square 实现 Shape 接口的 GetArea 方法
func (s *Square)GetArea() float64 {
  return s.Width * s.High
}
// Square 实现 Shape 接口的 GetPerimeter 方法
func (s Square)GetPerimeter() float64 {
  return 2 * (s.Width + s.High)
}
///////////////////////////////////////////////////////////////////////
//3.矩形的典型特征就是宽和高

//矩形结构体
type Rectangle struct {
  Width float64
  High float64
}
// Rectangle 这里只实现了 Shape 接口的 GetArea 方法,没有实现 GetPerimeter 方法,所以因此Rectangle没有实现Shape接口。
func (r Rectangle)GetArea() float64 {
  return r.Width * r.High / 2
}

func main() {
  //声明接口变量,并实例化
  var shape Shape
  fmt.Println(shape) 
  shape = nil
  fmt.Println(shape) 
  shape = Circle{10} // 等号右侧表达式的值是Circle类型,Circle类型实现了Shape接口,因此可以赋值给Shape接口变量
  fmt.Println(shape) 

  shape = &Circle{10} // 等号右侧表达式的值是*Circle类型,*Circle类型实现了Shape接口,因此可以赋值给Shape接口变量
  fmt.Println("Circle实现了shape接口") 
  fmt.Println(fmt.Sprintf("输出: %f\n", shape.GetArea()))
  fmt.Printf("输出: %f\n", shape.GetArea())

  shape = &Square{10, 10} // 等号右侧表达式的值是*Square类型,*Square类型实现了Shape接口,因此可以赋值给Shape接口变量
  fmt.Println(shape) 

  //shape = Square{10, 10} // 编译错误:cannot use Square literal (type Square) as type Shape in assignment: Square does not implement Shape (GetArea method has pointer receiver)
  //因为等号右侧表达式的值是Square类型,Square类型并没有实现Shape接口(尽管*Square类型实现了Shape)
  //shape = Rectangle{10, 10} // 编译错误。 Rectangle 没有实现Shape接口。

  //接口变量可以赋值给相同类型的接口变量
  shape = Circle{10}
  var shape2 Shape = shape
  fmt.Println(shape2)
}

posted @ 2023-02-19 23:54  jinzi  阅读(3)  评论(0编辑  收藏  举报