Go语言创建类和对象
本文我们将学习如何创建类和对象。
结构体(Structs) − Go语言没有类。要在Go语言中创建对象,我们可以指定结构体并在其中存储键值对。结构体是一种用户自定义的数据类型,用于将数据组合在一起。存储的值可以具有相同或不同的数据类型。
语法
定义结构体的语法如下:
type name_of_struct struct {
name_1 type_1
name_2 type_2
name_3 type_3
name_4 type_4 …
}
一个新的结构体以`type`关键字开头,用于指定我们正在定义一个新类型,然后是结构体的名称和`struct`关键字,用于指定我们正在定义一个新结构体。结构体可以将相同或不同数据类型的不同值存储在一起。
对象 − 对象用于以键值对的形式将数据封装在一起。创建对象的方法有很多种。我们将在本文中讨论其中一些方法。
示例1
Go语言创建类代码:
package main
// fmt package allows us to print anything on the screen
import "fmt"
// defining a structure with name myStrunct
type Student struct {
// defining values of struct
name string
rollNo int
admissionNo int
class int
}
// function to student data type
func (s Student) printDetails() {
fmt.Println("Student Details...\n")
// printing the structure details
fmt.Println("Student name is:", s.name)
fmt.Println("Student Admission number is:", s.admissionNo)
fmt.Println("Student Class is:", s.class)
fmt.Println("Student Roll number is:", s.rollNo)
}
func main() {
// defining a variable name stud_1 of type Student
var stud_1 = Student{name: "Nitin Sharma", rollNo: 21, class: 7, admissionNo: 2485}
// calling the printDetails() function to print the structure details on the screen.
stud_1.printDetails()
}
输出
Student Details... Student name is: Nitin Sharma Student Admission number is: 2485 Student Class is: 7 Student Roll number is: 21
以上代码描述
首先,我们需要导入`fmt`包。此包允许我们在屏幕上打印任何内容。
然后我们定义了一个名为`Student`的结构体,它以字符串和整数的形式存储学生的凭据,例如姓名、学号、班级和入学号。
然后我们定义结构体应具有的属性以及它应采用的数据类型。
接下来,我们定义了一个函数,该函数以`student`类型的变量作为参数,并使用`fmt.Println()`函数在屏幕上打印学生的详细信息。
调用`main()`函数。
创建一个`stud_1`变量,并在其中存储`Student`类型的键值对。
调用`printDetails()`函数。
使用`fmt.Println()`函数在屏幕上打印详细信息。
示例2:Go语言创建对象
获得结构体后,下一步是从结构体中创建对象。有多种方法可以从结构体创建对象。
方法1:向结构体传递逗号分隔的值
获得结构体后,下一步是从结构体中创建对象。有多种方法可以从结构体创建对象。
示例
创建对象最简单的方法是按照在结构体中定义的顺序直接传递值。
package main
import "fmt"
// fmt package allows us to print anything on the screen
// defining a structure named Employee and adding some data to it
type Employee struct {
Name string
Age int
Designation string
Salary int
}
// calling the main() function
func main() {
// creating an object named newEmp by passing various comma
//separated values to it.
var newEmp = Employee{"Nitin", 27, "Developer", 40}
// printing the age of the employee from the newEmp object
fmt.Println("The age of the employee is:", newEmp.Age)
// printing the name of the employee from the newEmp object
fmt.Println("The name of the employee is:", newEmp.Name)
}
输出
The age the of employee is: 27 The name the of employee is: Nitin
描述
首先,我们导入`fmt`包,它允许我们在屏幕上打印任何内容。
接下来,我们创建一个名为`Employee`的结构体,并在其中定义键,例如姓名、年龄、薪水等。
然后我们调用`main()`函数。
从`Employee`类创建一个名为`newEmp`的对象,并将值作为逗号分隔的值传递给键。
现在,我们的`newEmp`对象包含我们可以使用`fmt.Println()`函数在屏幕上打印的所有必要数据。
要访问对象的任何属性,我们需要在指定对象名称后使用“.”表示法,后跟我们要访问的属性。
注意 − 如果我们在创建对象时未指定结构体中定义的任何属性的值,则会为这些属性选择一些默认值。以下是一些默认值。
字符串值默认为空字符串“”。
整数值默认为0。
布尔值默认为false。
方法2:使用`new`关键字创建对象
示例
创建对象的另一种常用方法是使用`new`关键字。让我们看一个例子以了解更多信息。
package main
import "fmt"
// fmt package allows us to print anything on the screen
// defining a structure named Employee and adding some data to it
type Employee struct {
Name string
Age int
Designation string
Salary int
}
// calling the main() function
func main() {
// creating an empty object named newEmp.
var newEmp = new(Employee)
// assigning values to newEmp object
newEmp.Name = "Ram"
newEmp.Age = 30
// printing the age of the employee from the newEmp object
fmt.Println("The age of employee is:", newEmp.Age)
// printing the name of the employee from the newEmp object
fmt.Println("The name of employee is:", newEmp.Name)
// printing the default values
fmt.Println("The default values for salary is:", newEmp.Designation, newEmp.Salary)
}
输出
The age of employee is: 30 The name of employee is: Ram The default values for salary is: 0
结论
我们已经成功编译并执行了Go语言代码来创建类和对象,并附带示例。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP