Go语言程序:确定对象的类


Go语言中的对象是什么?

在Go编程中,对象是在结构体上定义的,并将数据的键值对一起存储。有很多方法可以创建对象。结构体是一种用户定义的类型,它将字段(也称为属性或数据成员)和方法组合在一起。可以使用`type`关键字定义结构体,后跟结构体名称和定义结构体字段的结构体字面量。

方法一:使用用户自定义函数

算法

  • 步骤1 − 首先,我们需要导入`fmt`包。

  • 步骤2 − 然后,我们定义了一个名为`Student`的结构体,它以字符串和整数的形式存储学生的凭据,例如姓名、学号、班级和入学号。

  • 步骤3 − 然后我们定义结构体应该具有的属性及其应采用的数据类型。

  • 步骤4 − 接下来,我们定义了一个函数,该函数将`student`类型的变量作为参数,并使用`fmt.Println()`函数在屏幕上打印学生的详细信息。

  • 步骤5 − 调用`main()`函数。

  • 步骤6 − 在上面声明的`Student`类上创建一个名为`stud_1`的对象,并将`Student`类型的键值对存储在其中。

  • 步骤7 − 调用`printDetails()`函数。

  • 步骤8 − 使用`fmt.Println()`函数在屏幕上打印详细信息。

示例

在这个例子中,我们将编写一个Go语言程序来创建一个类的对象。我们将创建一个类并从中定义一个对象。此外,我们将数据存储到对象中并在屏幕上打印出来。

package main
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")
   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() {
   var stud_1 = Student{name: "Nitin Sharma", rollNo: 21, class: 7, admissionNo: 2485}
   stud_1.printDetails()
}

输出

Student Details...

Student name is: Nitin Sharma
Student Admission number is: 2485
Student Class is: 7
Student Roll number is: 21

方法二:使用结构体和`new`关键字

在这种方法中,我们在第一个示例中使用结构体,在第二个示例中使用`new`关键字来确定Go编程中对象的类。

算法

  • 步骤1 − 首先,我们导入`fmt`包。

  • 步骤2 − 接下来,我们创建一个名为`Employee`的结构体,并在其中定义键,例如姓名、年龄、薪水等。

  • 步骤3 − 然后我们调用`main()`函数。

  • 步骤4 − 从`Employee`类创建一个名为`newEmp`的对象,并将值作为逗号分隔的值传递给键。

  • 步骤5 − 我们的`newEmp`对象包含所有必要的数据,我们可以使用`fmt.Println()`函数在屏幕上打印出来。

  • 步骤6 − 要访问对象的任何属性,我们需要在指定对象名称后使用“.”表示法,然后是我们要访问的属性。

示例1

Go程序:通过向结构体传递逗号分隔的值来创建对象的类

package main
import "fmt"

// defining a structure named Employee and adding some data to it
type Employee struct {
   Name string
   Age int
   Designation string
   Salary int
}
func main() {

   // creating the object named newEmp
   var newEmp = Employee{"Nitin", 27, "Developer", 40}
   fmt.Println("The age of employee is:", newEmp.Age)
   fmt.Println("The name of employee is:", newEmp.Name)
}

输出

The age of employee is: 27
The name of employee is: Nitin

示例2

使用`new`关键字是创建对象的另一种流行方法。

package main
import "fmt"

// defining a structure named Employee and adding some data to it
type Employee struct {
   Name string
   Age int
   Designation string
   Salary int
}
func main() {

   // creating an empty object named newEmp.
   var newEmp = new(Employee)
   newEmp.Name = "Ram"
   newEmp.Age = 30
   fmt.Println("The age of employee is:", newEmp.Age)
   fmt.Println("The name of employee is:", newEmp.Name)
   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语言程序来确定对象的类以及示例。在这里,我们展示了三种方法,通过这些方法,我们可以从Go语言中的结构体或类创建对象,以及访问此类对象中存在的数据的方法。

更新于:2023年2月16日

95 次浏览

启动你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.