Go语言程序创建简单类?


在本教程中,我们将讨论在 Go 编程中创建简单类的方法。

但是,在编写代码之前,让我们简要讨论一下类的概念。

什么是类?

类是创建对象的模板或蓝图。类将程序的所有元素绑定在一起,它是面向对象编程的重要组成部分。

然而,Go 语言**不支持“class”关键字**,这与其他面向对象语言(如 Java、C++ 等)不同。

但是,这并不限制 Go 使用类的功能。Go 在不使用“class”关键字的情况下保持了类的概念。

Go 语言中的 Struct 关键字

  • Struct 关键字是 Go 中一个强大的工具,类似于类。这意味着我们可以使用 struct 在 Go 中创建类,而无需实际使用“class”关键字。

  • 通过定义 struct,我们可以定义一个类,并且通过使用 struct 类型,我们还可以实现其方法。Struct 关键字的行为与类非常相似。

  • Struct,也称为结构体,是将不同的字段组合成一个字段的集合。它们用于将数据组合为一个。

  • Struct 是用户定义的且可变的。

示例

假设您想存储组织中员工的详细信息。这些详细信息将包括员工姓名、员工 ID、地址、性别、年龄等不同数据类型的字段。

为了存储不同数据类型的多个值,可以使用 struct,其中 Employee 可以是 struct 名称,所有员工的详细信息可以是其字段。

type Employee struct {
   employeeName   string
   employeeID     int
   employeeEmail  string
   employeeSalary float
}

简单类的演示

创建 Employee Struct 来模拟类

算法

  • **步骤 1** - 创建一个名为“Employee”的 struct,并声明其字段(如员工姓名、员工 ID 等)及其数据类型。

  • **步骤 2** - 创建一个函数 PrintDetailsOfEmployee(),它将访问 Employee struct 的值并打印员工的详细信息。

  • **步骤 3** - 在主方法中,将值初始化为员工。这将以键值对的形式完成。

  • **步骤 4** - 通过调用 PrintDetailsOfEmployee() 函数打印员工的详细信息。

示例

package main

// fmt package allows us to print formatted strings
import "fmt"

// defining struct
type Employee struct {
	employeeName   string
	employeeID     int
	employeeGender string
	employeeEmail  string
	employeeSalary float32
}

// this method will access the values of struct Employee
func (emp Employee) PrintDetailsOfEmployee() {
	fmt.Println("Name : ", emp.employeeName)
	fmt.Println("ID: ", emp.employeeID)
	fmt.Println("Gender: ", emp.employeeGender)
	fmt.Println("Email Address : ", emp.employeeEmail)
	fmt.Println("Salary : ", emp.employeeSalary)
}

func main() {
     fmt.Println("Employee Details \n")
     // storing employee details
	var emp1 = Employee{employeeName: "Rahul Singh", employeeID: 50062, employeeGender: "Male", employeeEmail: "rahul@hello.com", employeeSalary: 65000}
	var emp2 = Employee{employeeName: "Shreya Goel", employeeID: 50132, employeeGender: "Female", employeeEmail: "shreya@hello.com", employeeSalary: 57000}

     // printing employee details
	emp1.PrintDetailsOfEmployee()
	emp2.PrintDetailsOfEmployee()
}

输出

Employee Details 

Name :  Rahul Singh
ID:  50062
Gender:  Male
Email Address :  rahul@hello.com
Salary :  65000

Name :  Shreya Goel
ID:  50132
Gender:  Female
Email Address :  shreya@hello.com
Salary :  57000

代码描述

  • var emp1 = Employee{employeeName: "Rahul Singh", employeeID: 50062, employeeGender: "Male", employeeEmail: "rahul@hello.com", employeeSalary: 65000} − 这里,我们以键值对的形式声明 Employee 的值,其中键是员工字段,值是员工的信息。

创建 Rectangle Struct 来模拟类

  • **步骤 1** - 创建一个名为“Rectangle”的 struct,并声明其字段(如长度和宽度)及其数据类型。

  • **步骤 2** - 创建一个函数 CalculateArea() 来打印矩形的详细信息,并使用公式长度 * 宽度计算其面积并打印。

  • **步骤 3** - 在主方法中,将值初始化为矩形 struct,并以键值对的形式存储其值。

  • **步骤 4** - 通过调用 CalculateArea() 函数打印矩形的长度、宽度和面积。

示例

package main

// fmt package allows us to print formatted strings
import "fmt"

// defining struct
type Rectangle struct {
	length  int
	breadth int
}

// this method will access the values of struct Rectangle
func (rect Rectangle) CalculateArea() {
	fmt.Println("Length : ", rect.length)
	fmt.Println("Breadth : ", rect.breadth)
	fmt.Println("Therefore, Area : ", rect.length*rect.breadth)
}

func main() {
	fmt.Println("Rectangle Details \n")
	var rect1 = Rectangle{length: 5, breadth: 2}
	var rect2 = Rectangle{length: 25, breadth: 10}

	rect1.CalculateArea()
	rect2.CalculateArea()
}

输出

Rectangle Details 

Length :  5
Breadth :  2
Therefore, Area :  10

Length :  25
Breadth :  10
Therefore, Area :  250

代码描述

  • var rect1 = Rectangle{length: 5, breadth: 2} − 这里,我们以键值对的形式声明 Rectangle 的值,其中键是矩形字段,值是矩形的信息。

结论

这就是在 Go 编程中创建类的方法,我们通过两个示例详细说明了 Go 中没有“class”关键字如何不限制我们使用类的功能。您可以使用这些教程探索更多关于 Go 编程的信息。

更新于: 2022-12-28

693 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.