Golang 创建一个简单类
在本教程中,我们将讨论在 Golang 编程中创建一个简单类的方法。
不过,在编写代码之前,我们先简要讨论一下类的概念。
类是什么
类是创建对象的模板或蓝图。类将一个程序的所有元素绑定在一起,是面向对象编程的重要组成部分。
然而,Go 语言不支持像 Java、C++ 等面向对象语言中的 ‘class’ 关键字。
然而,这并不限制了 Go 使用类的功能。Go 在不使用 ‘class’ 关键字的情况下仍然支持类的概念。
Go 语言中的 Struct 关键字
- Struct 关键字是 Go 中一个类似于类的强大工具。这意味着我们可以使用 struct 在 Go 中创建一个类,而无需实际使用 ‘class’ 关键字。
-
通过定义一个 struct,我们可以定义一个类,并且通过使用 struct 类型,我们还可以实现其方法。struct 关键字的行为与类的行为非常相似。
-
结构体,也称为结构,是将不同字段集合到一个字段中的集合。它们用于将数据分组为单个整体。
-
结构体是用户自定义且可变的。
示例
假设您想要存储组织中员工的详细信息。详细信息包括员工姓名、员工 ID、地址、性别、年龄等不同数据类型的字段。
为了存储不同数据类型的多个值,可以使用一个结构体,其中 Employee 可以是结构体名称,而所有员工的详细信息可以是其字段。
type Employee struct {
employeeName string
employeeID int
employeeEmail string
employeeSalary float
}
简单类的示例
创建 Employee 结构体类似于类
步骤
- 步骤1 - 创建一个名为 ‘Employee’ 的结构体,并声明字段,如员工姓名、员工ID等,以及它们的数据类型。
-
步骤2 - 创建一个函数 PrintDetailsOfEmployee(),该函数将访问 Employee 结构体的值并打印出员工的详细信息。
-
步骤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: "男性", employeeEmail: "rahul@hello.com", employeeSalary: 65000}
− 这里,我们以键值对的形式声明了员工的信息,其中键是员工字段,值是员工的信息。
创建一个类似的矩形结构体
-
步骤1 − 创建一个名为’Rectangle’的结构体,并声明长度和宽度等字段及其数据类型。
-
步骤2 − 创建一个函数CalculateArea()来打印矩形的详细信息,并使用公式长度*宽度来计算其面积并打印出来。
-
步骤3 − 在主方法中,给矩形结构体的字段赋初值,并将其值以键值对的形式存储。
-
步骤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的值,其中键是矩形的字段,值是矩形的信息。
结论
这就是关于在Golang编程中创建类的全部内容,我们用两个示例详细说明了在Go中没有”class”关键字不会限制我们使用类的功能。您可以使用这些教程进一步了解Golang编程。