Golang程序 创建一个简单的类
在本教程中,我们将讨论在Golang编程中创建一个简单类的方法。
然而,在编写代码之前,让我们简单地讨论一下类的概念。
什么是类
类是一个创建对象的模板或蓝图。类将程序中的所有元素结合在一起,它是面向对象编程的一个重要部分。
然而,Go语言 不支持 “类 “关键字 ,这与其他语言如Java、C++等面向对象的语言不同。
然而,这并不限制Go使用类的功能。Go语言保持了类的概念,但没有使用 “类 “这个关键词。
Go语言中的Struct关键字
- Struct关键字是Go语言中的一个强有力的工具,它类似于类。这意味着我们可以在Go语言中使用struct创建一个类,而不需要实际使用 “类 “关键字。
-
通过定义一个结构,我们可以定义一个类,通过使用结构类型,我们也可以实现其方法。结构关键字的行为与类的行为非常相似。
-
结构,也被称为结构,是不同字段的集合,是一个单一字段。它们用于将数据分组为一个整体。
-
结构是用户定义的,并且是可变的。
例子
假设你想存储一个组织的雇员的详细信息。这些细节将包括不同数据类型的字段,如雇员姓名、雇员ID、地址、性别、年龄等。
为了存储不同数据类型的多个值,可以使用一个结构,其中Employee可以是结构名称,所有雇员的详细信息可以是其字段。
type Employee struct {
employeeName string
employeeID int
employeeEmail string
employeeSalary float
}
一个简单类的演示
创建雇员结构以类似于一个类
算法
- 第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:"Male", employeeEmail:"rahul@hello.com", employeeSalary: 65000}
– 在这里,我们以key和value的形式声明Employee的值,其中key是雇员字段,value是雇员的信息。
创建一个类似于类的矩形结构
-
第1步– 创建一个名为 “矩形 “的结构,并声明长度和宽度等字段以及它们的数据类型。
-
第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} – 这里,我们以key和values的形式声明Rectangle的值,其中key是矩形字段,value是矩形的信息。
总结
这就是关于在Golang编程中创建类的全部内容,我们通过两个例子阐述了Go中没有 “类 “这个关键字并不限制我们使用类的功能。