Go 语言中的结构体和“面向对象”

本文主要来讲解一下 Go 语言的结构体数据类型,以及 Go 语言中的”面向对象”,为什么要加双引号呢?因为 Go 语言中没有明确的面向对象的概念,当然也没有明确的面向过程的概念。面向哪一方面编程完全取决于你如何使用,如果你想要面向对象编程,Go 语言中提供了相对应的解决方案来模拟面向对象。

 

1. 结构体类型

想要模拟面向对象就首先要了解一下 Go 语言中的结构体类型。当你需要两个以上的基本数据类型或自定义类型来声明定义函数的时候,你就可以使用结构体。结构体变量使用struct{...}的形式来定义。

代码示例:

代码块
  • package main
  • 2
  • import (
  • 4          "fmt"
  • 5          "reflect"
  • )
  • 7
  • func main() {
  • 9          var student1 struct {
  • 10                Name string
  • 11                Age int
  • 12        }
  • 13        student1.Name = "Codey"
  • 14        student1.Age = 18
  • 15
  • 16        fmt.Println("student1数据类型:", reflect.TypeOf(student1))
  • 17        fmt.Println("student1的值:", reflect.ValueOf(student1))
  • 18
  • 19        student2 := struct {
  • 20                Name string
  • 21                Age int
  • 22        }{
  • 23                Name: "Codey",
  • 24                Age: 18, //这个逗号千万不能忘记,若是和大括号同行,这个逗号才可以省略
  • 25        }
  • 26
  • 27        fmt.Println("student2数据类型:", reflect.TypeOf(student2))
  • 28        fmt.Println("student2的值:", reflect.ValueOf(student2))
  • 29  }
  • 第 9~12 行:声明一个结构体变量 student1,其属性为 string 类型的 Name 和 int 类型的 Age。
  • 第 13 行:给结构体变量的 Name 属性赋值。
  • 第 14 行:给结构体变量的 Age 属性赋值。
  • 第 19~25 行:定义一个结构体变量 student2,其属性为 string 类型的 Name 和 int 类型的 Age,并赋初值。

执行结果:

图片描述

 

2. 使用自定义类型来模拟对象

使用自定义数据类型的方式来自定义一个结构体类型,这样就可以达到定义一个对象的相同效果。

代码示例:

代码块
  • package main
  • 2
  • import (
  • 4            "fmt"
  • 5            "reflect"
  • )
  • 7
  • type Student struct {
  • 9            Name string
  • 10          Age int
  • 11  }
  • 12
  • 13  func main() {
  • 14          var student1 Student
  • 15          student1.Name = "Codey"
  • 16          student1.Age = 18
  • 17
  • 18          fmt.Println("student1数据类型:", reflect.TypeOf(student1))
  • 19          fmt.Println("student1的值:", reflect.ValueOf(student1))
  • 20  }
  • 第 8~11 行:自定义一个结构体数据类型 Student。也可以解读为,创建一个”Student类”;
  • 第 14 行:声明一个 Student 类型的变量 student1。也可以解读为,创建一个”Student对象”。

执行结果:

图片描述

 

3. 使用函数来模拟面向对象的方法

Go 语言中的函数为自定义数据类型提供了一种特别的使用方式,形如func(变量名 自定义数据类型)函数名(...){...}。函数名前可以接收一个自定数据类型的参数,参数传递的形式为自定义数据类型变量.函数名()的形式传递,这样外表看起来就和面向对象编程一致了。

代码示例:

代码块
  • package main
  • 2
  • import (
  • 4            "fmt"
  • )
  • 6
  • type Student struct {
  • 8            Name string
  • 9            Age int
  • 10  }
  • 11
  • 12  func newStudent(name string, age int) Student {
  • 13          return Student{Name: name, Age: age}
  • 14  }
  • 15  func (s Student) PrintAge() {
  • 16          fmt.Println(s.Age)
  • 17  }
  • 18  func main() {
  • 19          student1 := newStudent("Codey", 18)
  • 20          student2 := newStudent("Tom", 19)
  • 21          fmt.Println("student1年龄:")
  • 22          student1.PrintAge()
  • 23          fmt.Println("student2年龄:")
  • 24          student2.PrintAge()
  • 25  }
  • 第 15~17 行:打印传入的 Student 自定义类型参数 Age 属性。也可以解读为创建一个打印学生”对象”年龄的”方法”;
  • 第 22 行:使用自定义类型变量 student1 为参数调用 PrintAge。也可也解读为 student1 “对象”调用PrintAge “方法”。

 

4. 小结

本文主要介绍了 Go 语言中另类的面向对象。Go 语言中虽然没有真正意义上的面向对象,但是它可以通过结构体+自定义数据类型的方式对面向对象进行模拟。所以可见 Go 语言是一个非常灵活的语言,它的走向和创造力完全取决于开发者的思维方式。

文章来源于网络,侵删!

相关新闻

历经多年发展,已成为国内好评如潮的Linux云计算运维、SRE、Devops、网络安全、云原生、Go、Python开发专业人才培训机构!