找回密码
 立即注册
首页 业界区 业界 Golang基础笔记九之方法与接口

Golang基础笔记九之方法与接口

金娅鸣 2025-10-5 14:36:46
本文首发于公众号:Hunter后端
原文链接:Golang基础笔记九之方法与接口
本篇笔记介绍 Golang 里方法和接口,以下是本篇笔记目录:

  • 方法
  • 接口
  • 用结构体实现类的功能
1、方法

首先介绍一下方法。
方法是与特定类型关联的函数,我们在实现一个函数前,绑定一个类型,就实现了这个类型的方法。
比如我们想实现一个结构体的方法,可以如下操作:
  1. type Person struct {
  2.     Name string
  3.     Age  int
  4. }
  5. func (person Person) fmtPersonInfo() {
  6.     fmt.Printf("person name is %s, age is %d\n", person.Name, person.Age)
  7. }
复制代码
在上面的操作中,我们就为 Person 这个结构体绑定了一个方法,而其调用也很简单,就是实例化一个 Person 结构体后,就可以对其进行调用:
  1. person := Person{Name: "Hunter", Age: 28}
  2. person.fmtPersonInfo()
复制代码
方法支持的类型

方法支持绑定的类型有结构体、指针类型、接口类型以及自定义类型,但是不支持绑定 Golang 内置的类型包括 int、slice、map 等。
方法绑定到指针类型

前面介绍了方法绑定到结构体上,这里再介绍一个绑定到指针类型上,还是前面的 Person 结构体,绑定到其指针上,来实现更改 Age 字段的操作:
  1. func (person *Person) ChangeAge(age int) {    person.Age = age}person := Person{Name: "Hunter", Age: 28}
  2. person.fmtPersonInfo()person.ChangeAge(18)person.fmtPersonInfo()
复制代码
第二次打印信息就可以看到 person 的 age 已经发生了变化。
这里需要注意一点,Person 结构体是值类型,如果绑定的是其结构体本身,而非其指针类型,在方法中对其更改后,并不会影响结构体本身,比如下面的操作:
  1. func (person Person) NewChangeAge(age int) {    person.Age = age}person := Person{Name: "Hunter", Age: 28}
  2. person.fmtPersonInfo()person.NewChangeAge(18)person.fmtPersonInfo()
复制代码
可以看到,这里调用 NewChangeAge() 方法后,没有对 person 这个结构体本身进行更改。
方法绑定到自定义类型

而如果想要绑定 int、slice、map 等内置类型,可以通过方法支持的自定义类型来绑定。
比如我们想要实现使用 slice 绑定一个打印其长度的方法,可以通过自定义类型设置一个别名,通过别名来绑定一个方法:
  1. type MySlce []int
  2. func (mySlice MySlce) printSliceLength() {
  3.     fmt.Printf("mySliceLength is %d\n", len(mySlice))
  4. }
  5. slice := MySlce{1, 2, 3}
  6. slice.printSliceLength()
复制代码
2、接口

1. 接口的定义和实现

接口是一组方法签名的集合,任何类型只要实现了接口中的所有方法,就被认为实现了该接口。
比如下面我们定义了一个形状的接口,内部有面积和周长两个空方法:
  1. type Shape interface {
  2.     Area() float64
  3.     Perimeter() float64
  4. }
复制代码
这样我们就定义了一个接口。
而如果我们要实现这个接口,只需要实现这个接口里的两个方法 Area() 和 Perimeter() 就是实现了这个接口,这个过程是隐式的,不需要显式声明或者绑定。
接下来我们定义 Rectangle 和 Circle 两个结构体,并且实现 Area() 和 Perimeter() 两个方法:
  1. type Rectangle struct {
  2.     Width, Height float64
  3. }
  4. func (r Rectangle) Area() float64 {
  5.     return r.Width * r.Height
  6. }
  7. func (r Rectangle) Perimeter() float64 {
  8.     return 2 * (r.Width + r.Height)
  9. }
  10. type Circle struct {
  11.     radius float64
  12. }
  13. func (c Circle) Area() float64 {
  14.     return 3.14 * c.radius * c.radius
  15. }
  16. func (c Circle) Perimeter() float64 {
  17.     return 2 * 3.14 * c.radius
  18. }
复制代码
我们已经分别用 Rectangle 和 Circle 这两个结构体实现了 Shape 接口。
那么这个接口在这里有什么作用呢,我们可以实现一个函数,接收接口类型的参数,那么实现了这个接口的结构体都可以作为传入:
  1. func PrintShapeInfo(s Shape) {
  2.     fmt.Println("Area: ", s.Area())
  3.     fmt.Println("Perimeter: ", s.Perimeter())
  4. }
  5. func main() {
  6.     r := Rectangle{Width: 2, Height: 3}
  7.     PrintShapeInfo(r)
  8.     c := Circle{Radius: 5}
  9.     PrintShapeInfo(c)
  10. }
复制代码
2. 类型断言

类型断言用于检查接口值的底层具体类型,并提取该类型的值,其用法示例如下:
  1. value, ok := interfaceValue.(ConcreteType)
复制代码

  • value 是转换后的具体类型值
  • ok 是一个布尔值,表示是否断言成功
  • interfaceValue 是接口类型的变量
  • ConcreteType 是目标具体类型。
    比如我们可以修改 PrintShapeInfo 函数,在内部对其进行类型断言:
  1. func PrintShapeInfo(s Shape) {
  2.     circle, ok := s.(Circle)
  3.     if ok {
  4.         fmt.Println("ths shape is circle, the area is: ", circle.Area())
  5.     } else {
  6.         fmt.Println("this shape is not circle")
  7.     }
  8.     fmt.Println("Area: ", s.Area())
  9.     fmt.Println("Perimeter: ", s.Perimeter())
  10. }
复制代码
3. 空接口

空接口(interface{}) 可以表示任何类型的值,常用于处理不确定类型的数据。
比如我们想打印一个输入的变量,但是这个变量的类型不确定,我们可以使用空接口来处理这种情况。
  1. func PrintType(a interface{}) {
  2.     switch v := a.(type) {
  3.     case int:
  4.         fmt.Println("this is int: ", v)
  5.     case float64:
  6.         fmt.Println("this is float64: ", v)
  7.     case string:
  8.         fmt.Println("this is string: ", v)
  9.     default:
  10.         fmt.Println("this is other type: ", v)
  11.     }
  12. }
  13. func main() {
  14.     PrintType(1)
  15.     PrintType(3.4)
  16.     PrintType("abc")
  17. }
复制代码
3、用结构体实现类的功能

在 Golang 里没有类的相关定义,但是我们可以使用结构体和方法的组合来实现类的相关特性。
1. 封装

我们可以通过结构体字段的首字母大小写控制访问权限,然后提供公共方法来操作私有字段。
在结构体中,大写开头的字段为公开字段,小写开头的字段为私有字段。
我们用下面的示例来展示一下用结构体和方法来实现封装功能。
文件目录如下:
  1. .
  2. ├── main.go
  3. ├── service
  4. │   └── person_operation.go
复制代码
其中 person_operation.go 的内容如下:
  1. package service
  2. type Person struct {
  3.     Name   string
  4.     Age    int
  5.     gender string
  6. }
  7. func (p *Person) SetGender(gender string) {
  8.     p.gender = gender
  9. }
  10. func (p *Person) GetGender() string {
  11.     return p.gender
  12. }
复制代码
其中,Person 这个结构体的 Name 和 Age 字段首字母都为大写,为公共字段,而 gender 首字母为小写,在 main.go 里不能直接引用,所以下面定义了两个公有接口提供设置和访问。
以下是 main.go 里的内容:
  1. package main
  2. import (
  3.     "fmt"
  4.     "go_proj/service"
  5. )
  6. func main() {
  7.     person := service.Person{
  8.         Name: "张三",
  9.         Age:  18,
  10.         // gender: "男",  // gender是私有属性,不能直接访问
  11.     }
  12.     // fmt.Println(person.gender) // gender是私有属性,不能直接访问
  13.     fmt.Println(person.GetGender())
  14.     person.SetGender("男")
  15.     fmt.Println(person.GetGender())
  16. }
复制代码
在这里,gender 字段在 Person 定义和访问的时候都不能直接操作,需要通过设置的方法来进行定义以及访问。
2. 继承

我们可以通过结构体的嵌套来实现继承,比如下面新建一个 Chinese 结构体:
  1. type Chinese struct {
  2.     Person
  3. }
复制代码
然后我们定义的 Chinese 实例可以调用 Person 结构体的方法:
  1. chinese := service.Chinese{
  2.     Person: service.Person{
  3.         Name: "张三",
  4.         Age:  18,
  5.     },
  6. }
  7. chinese.SetGender("男")
  8. fmt.Println(chinese.GetGender())
复制代码
3. 多态

多态则是同一方法名在不同的类型中有不同的实现,这个操作在前面介绍接口的就已经实现过了,这里不再做赘述。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册