Go語言程式演示類繼承


在本文中,我們將學習如何使用 Go 程式語言在類中演示繼承。

Go 語言中的繼承 - 面向物件程式設計中的一個關鍵概念是繼承,它指的是將超類的屬性傳遞給基類。由於 Go 語言不支援類,因此繼承透過結構體嵌入實現。由於結構體不能直接擴充套件,我們必須使用組合的概念來使用結構體建立新物件。因此,可以肯定地說,Go 語言不支援繼承。

示例 1

Go語言程式演示類繼承

以下程式碼展示瞭如何在 Go 程式語言中演示繼承。

package main

import (
   "fmt"
)
// fmt package allows us to print anything

// defining a struct named games and defining a string variable in it
type games struct {
   new string
}

// function to return the string variable of the games struct
// this function is defined on the games struct and returns the string value
func (games games) AllGames() string {

   // returns new variable
   return games.new
}

// declaring another struct named cricket
type Cricket struct {

   // embedding the games struct in the cricket struct
   games
}

// declaring a struct named hockey
type hockey struct {
   // embedding the games struct in the hockey struct
   games
}

// calling the main function
func main() {

   // creating an instance to the cricket struct
   I1 := Cricket{

      // child struct can directly
      // access base struct variables
      games{
         new: "ICC tournaments",
      },
   }
   // storing the result in a variable called result
   result := I1.AllGames()

   // Accessing child struct directly from struct methods
   // printing base method using child
   fmt.Println("New game is:", result)

   // creating another instance to the hockey struct
   I2 := hockey{
      games{
         new: "Hockey tournaments",
      },
   }

   // Accessing child struct directly from struct methods
   // printing base method using child
   fmt.Println("New game is:", I2.AllGames())
}

輸出

New game is: ICC tournaments
New game is: Hockey tournaments

描述

  • 首先,我們需要匯入 fmt 包,它允許我們在螢幕上列印任何內容。

  • 然後我們需要定義一個結構體。這將是父結構體,相當於父類。此外,我們還為該結構體定義了一個函式,該函式將錦標賽名稱作為字串返回。

  • 此外,我們建立了兩個名為 cricket 和 hockey 的新結構體,這些結構體繼承了名為 games 的父結構體的所有屬性,並將成為子結構體或類。

  • 呼叫 main 函式,這是我們程式的起點,程式碼從這裡開始執行。

  • 建立 cricket 結構體的例項並定義其中存在的新字串。請注意,名為 new 的字串最初未在此結構體中定義,而是在 games 結構體中定義,我們能夠使用該結構體屬性的唯一原因是我們在 cricket 結構體中繼承了其屬性。

  • 現在呼叫為 games 結構體定義的 AllGames() 函式。

  • 將它返回的值儲存在一個名為 result 的變數中,並使用 fmt.Println() 在螢幕上列印它。

  • 透過建立 hockey 結構體的例項並在螢幕上列印結果來重複上述步驟。

示例 2

Go 語言程式演示類多重繼承

package main

import (
   "fmt"
)

// fmt package allows us to print anything on the screen

// declaring first struct and defining a string type property in it
type first struct {

   // declaring struct variable
   base_one string
}

// declaring second struct and defining a string type property in it
type second struct {
   // declaring struct variable
   base_two string
}

// defining the function to the first struct naming PrintBase()
// this function returns a string
func (f first) printBase1() string {

   // returning the result
   return f.base_one
}

// defining the function to the second struct naming PrintBase()
// this function returns a string
func (s second) printBase2() string {

   // returning the result
   return s.base_two
}

// defining the child struct
// inheriting the properties of first and second struct to it
type child struct {

   // inheriting the parent structs first and second
   first
   second
}

// calling the main function
func main() {

   // creating an instance to the child struct
   c1 := child{

      // assigning values to parent struct through instance of child struct
      first{
         base_one: "\nmango",
      },
      second{
         base_two: "apple\n",
      },
   }

   // child struct can directly
   // access base struct methods

   // calling the function defined on the first and second struct using the child structs
   fmt.Println("Printing the values by calling the method defined on first and second struct using instance defined on child struct")
   fmt.Println(c1.printBase1())
   fmt.Println(c1.printBase2())
}

輸出

Printing the values by calling the method defined on first and second struct using instance defined on child struct

mango
apple

描述

  • 首先,我們需要匯入 fmt 包,它允許我們在螢幕上列印任何內容。

  • 然後我們定義了兩個名為 first 和 second 的結構體。它們將是父結構體。此外,我們還為這些結構體定義了一個函式,該函式將返回一個字串。

  • 然後,我們需要建立一個子結構體,該結構體將繼承父結構體的所有屬性。

  • 呼叫 main 函式。

  • 建立子結構體的例項並定義其中存在的父結構體屬性。請注意,該字串最初未在此結構體中定義,而是在 first 和 second 結構體中定義,我們能夠使用該結構體屬性的唯一原因是我們在子結構體中繼承了其屬性。

  • 現在呼叫 printBase() 函式。

  • 使用 fmt.Println() 在螢幕上列印它。

更新於: 2022-12-29

6K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.