Go語言程式演示如何在類中使用super關鍵字


在 Go 語言中,不存在“super”這個術語。“super”關鍵字用於面向物件程式語言,表示父類或被繼承的類。它通常用於訪問子類修改過的父類方法或屬性。本文將透過不同的示例說明如何在類中使用super關鍵字。

方法 1:使用子結構體和父結構體

在本方法中,我們將學習如何使用子結構體和父結構體在類中使用super關鍵字。這裡,父結構體嵌入到子結構體中。此外,子結構體包含一個PrintName函式,該函式在列印自己的名稱後,使用“child.Parent.PrintName()”行呼叫父結構體的PrintName方法。讓我們看看執行過程。

演算法

  • 步驟 1 − 建立一個名為main的包,並在程式中宣告fmt(格式化包),其中main生成可執行檔案Example:s,fmt幫助格式化輸入和輸出。

  • 步驟 2 − 建立一個名為“Parent”的結構體,並賦予其一個名為“name”的字串型別欄位。

  • 步驟 3 − 在Parent結構體上建立一個名為“PrintName”的方法,接收者是指向該結構體的指標。該過程列印“name”欄位的值。

  • 步驟 4 − 建立一個“Child”結構體,嵌入Parent結構體。Child結構體中還存在一個名為“age”的int型別欄位。

  • 步驟 5 − 在Child結構體上建立一個名為“PrintName”的方法,接收者是指向該結構體的指標。該過程首先列印Child結構體“name”欄位的值。然後使用“child.Parent.PrintName()”行呼叫嵌入的Parent結構體的“PrintName”函式。

  • 步驟 6 − 在“main”方法中建立一個指向Child結構體的指標,並將其欄位填充為值。

  • 步驟 7 − 要使用“super”關鍵字,請在Child結構體指標上呼叫“PrintName”函式。

  • 步驟 8 − 輸出將是子類和父類的名稱,使用fmt.Println()函式列印到控制檯,其中ln表示換行。

示例

在本示例中,我們將使用子結構體和父結構體來演示如何在Go語言的類中使用super關鍵字。

package main
import "fmt"

type Parent struct {
   name string   //create name in the parent struct which will be used to print the name of parent
}

func (parent *Parent) PrintName() {
   fmt.Println("Parent:", parent.name) //print parent name
}

type Child struct {
   Parent        
   age int
}

func (child *Child) PrintName() {
   fmt.Println("Child:", child.name)    //print child name
   child.Parent.PrintName() // calling the PrintName method of the parent
}

func main() {
   child := &Child{Parent: Parent{name: "vipul kukreja"}, age: 16}
   child.PrintName() //call the method PrintName for child
}

輸出

Child: vipul kukreja
Parent: vipul kukreja

方法 2:使用矩形結構體和正方形結構體

在本例中,我們將瞭解如何使用矩形結構體和正方形結構體使用super關鍵字。這裡,我們建立了一個名為“Shape”的介面,其中包含一個“Area”方法。該介面由“Rectangle”結構體實現,該結構體具有相同簽名的函式。除了嵌入“Rectangle”型別外,“Square”結構體還具有一個“Area”函式。“sq.Rectangle.Area()”行用於從“Square”結構體的“Area”方法呼叫嵌入的“Rectangle”結構體的“Area”方法。讓我們看看執行過程。

演算法

  • 步驟 1 − 建立一個名為main的包,並在程式中宣告fmt(格式化包),其中main生成可執行檔案Example:s,fmt幫助格式化輸入和輸出。

  • 步驟 2 − 建立一個方法簽名為shape()的介面Area。

  • 步驟 3 − 建立Rectangle結構體,包含兩個欄位——寬度和高度——以及一個用於實現介面的方法。Area(),計算矩形的面積。

  • 步驟 4 − 新增一個新的正方形結構體,在其中嵌入Rectangle。

  • 步驟 5 − 實現Square結構體的Area()方法,該方法使用Rectangle計算正方形的面積。

  • 步驟 6 − 在main方法中建立一個指向Square結構體的指標,並將其欄位填充為值。

  • 步驟 7 − 呼叫Square結構體引用的Area()函式以呼叫Square結構體的Area方法並列印正方形的面積。

  • 步驟 8 − 也呼叫Rectangle結構體指標上的Area()函式,這將列印矩形的面積並呼叫Rectangle結構體的Area方法。

  • 步驟 9 − 比較這兩個函式的輸出將顯示差異,它將使用fmt.Println()函式列印到螢幕上,其中ln表示換行。

示例

在本示例中,我們將使用矩形結構體和正方形結構體來演示如何在Go語言的類中使用super關鍵字。

package main
import "fmt"

type Shape interface {
   Area() float64 //create area method in the shape interface
}

type Rectangle struct {
   width, height float64 //create width and height in the rectangle struct
}

func (rect *Rectangle) Area() float64 {
   return rect.width * rect.height  //return area of rectangle
}

type Square struct {
   Rectangle
}

func (sq *Square) Area() float64 {
   return sq.Rectangle.Area() + (sq.width * 2)
}

func main() {
   sq := &Square{Rectangle{width: 10, height: 6}}
   fmt.Println("Area of Square:", sq.Area())              // calls the Square struct's Area method
   fmt.Println("Area of Rectangle:", sq.Rectangle.Area()) // calls the Rectangle struct's Area method
}

輸出

Area of Square: 80
Area of Rectangle: 60

結論

我們使用兩個不同的示例執行了在類中使用super關鍵字的程式。在第一個示例中,我們使用了子結構體和父結構體,在第二個示例中,我們使用了shape介面並使用矩形和正方形結構體實現了它。

更新於: 2023年2月20日

903 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.