Go语言程序:打印类的对象
什么是类的对象?
对象存储键值对数据。创建对象的方法有很多,本文将介绍其中几种。
本文将介绍两种在Go语言中打印类对象的方法。
方法一:使用结构体
此方法首先创建一些结构体,然后在main()函数中创建它们的实例。接下来,我们将属性存储到这些对象中,并在屏幕上打印它们。这可以通过以下步骤实现:
算法
步骤1 − 首先,我们需要导入fmt包。
步骤2 − 要创建一个类,请初始化一个新的struct类型并在其中定义我们希望在类中定义的属性。在这个例子中,我们创建了两个结构体,一个名为student,另一个名为Animals。
步骤3 − Student类定义了三个属性:Id、Name和Fees,而Animals类具有species、color和price属性。
步骤4 − 调用main()函数。这是我们程序的入口点。
步骤5 − 从Student类创建Student1、Student2和Student3对象,并将值赋给已初始化的键。现在使用fmt.Println()函数在屏幕上打印对象。
步骤6 − 同样,从animals类创建两个对象animal1和animal2,并将值赋给它们的键。使用fmt.Println()函数在屏幕上打印相应的对象。
示例
在这个程序中,我们将编写一个Go语言程序,使用结构体来打印类的对象。
package main
import "fmt"
// Creating a structure named Student
type Student struct {
Id int
Name string
Fees int
}
// Creating a class named Animals
type Animals struct {
species string
color string
price int
}
func main() {
// Creating different objects from the Student class
student1 := Student{Id: 101, Name: "Kapil", Fees: 10000}
student2 := Student{Id: 102, Name: "Amit", Fees: 12000}
student3 := Student{Id: 103, Name: "Arun", Fees: 15000}
// Printing the objects created above on the screen
fmt.Println("Student's Information:")
fmt.Println("\nStudent1:", student1)
fmt.Println("\nStudent2:", student2)
fmt.Println("\nStudent3:", student3)
fmt.Println()
animal1 := Animals{species: "Dog", color: "white", price: 10000}
animal2 := Animals{species: "Cat", color: "Red", price: 1000}
fmt.Println("Animal's Information:")
fmt.Println(animal1)
fmt.Println(animal2)
}
输出
Student's Information:
Student1: {101 Kapil 10000}
Student2: {102 Amit 12000}
Student3: {103 Arun 15000}
Animal's Information:
{Dog white 10000}
{Cat Red 1000}
方法二:使用new关键字
此方法创建并定义一个结构体,然后使用new关键字创建该结构体的对象。
算法
步骤1 − 导入fmt包。
步骤2 − 接下来,我们创建一个名为Employee的结构体,并在其中定义键,如name、age、salary等。
步骤3 − 然后我们调用main()函数。
步骤4 − 使用new关键字从Employee类创建一个名为newEmp的对象,并将值作为逗号分隔的值传递给键。
步骤5 − 现在我们的newEmp对象包含了所有必要的数据,我们可以使用fmt.Println()函数在屏幕上打印它。
步骤6 − 要访问对象的任何属性,我们需要在指定对象名称后使用“.”符号,后跟我们希望访问的属性。
注意 − 创建对象时,如果我们没有为结构体声明的任何属性提供值,则会选择一些默认值。以下是一些常见的默认值。
默认情况下,字符串值为""(空字符串)。
整数的输入值默认为0。
布尔值默认为false。
示例
在这个例子中,我们创建了一个Go语言程序,使用new关键字打印类的对象。
package main
import "fmt"
// Defining a structure named Employee
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
newEmp.Designation = "Senior Developer"
newEmp.Salary = 50000
fmt.Println("Printing the employee object")
fmt.Println(newEmp)
fmt.Println()
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.Salary)
}
输出
Printing the employee object
&{Ram 30 Senior Developer 50000}
The age of employee is: 30
The name of employee is: Ram
The default values for salary is: 50000
结论
我们已经成功编译并执行了一个Go语言程序,用于在屏幕上打印类对象以及示例。这里我们创建了两个示例。在第一个示例中,我们使用等号运算符创建对象并将数据存储到其中,而在第二个示例中,我们使用new关键字创建对象。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP