Golang 创建类和对象的程序

Golang 创建类和对象的程序

在本文章中,我们将学习如何创建类和对象。

  • 结构体 - Go语言没有类的概念。在Go编程语言中,我们可以使用结构体来创建对象并存储键值对。结构体是一种用户定义的数据类型,用于将数据存储在一起。所存储的值可以具有相同或不同的数据类型。

语法

定义结构体的语法如下:

type name_of_struct struct {
   name_1 type_1
   name_2 type_2
   name_3 type_3
   name_4 type_4 …
}

一个新的结构以类型关键字开始,用于指定我们正在定义一个新类型,然后跟着结构的名称和struct关键字,以指定我们正在定义一个新的结构。结构可以同时存储相同或不同数据类型的不同值。

  • 对象 - 对象用于以键值对的形式封装数据。创建对象的方式有很多种。我们将在本文中讨论其中一些。

示例1

Golang程序创建一个类代码-

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 LANG创建对象的程序

一旦我们得到了结构体,下一步是创建对象。有不同的方法可以从结构体创建对象。

方法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的对象,并将值作为逗号分隔的值传递给键。

  • 现在,our 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语言代码,创建了一个类和一个对象,并提供了示例。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程