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.