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關鍵字建立物件。

更新於:2023年2月16日

830 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.