Golang程序 显示类中继承性

在这篇文章中,我们将学习用go编程来显示类中的继承性。

Golang中的继承 --面向对象编程的关键思想之一是继承,它指的是将超类的属性传递给基类。由于Golang不支持类,继承是通过结构嵌入来实现的。由于结构不能直接扩展,我们必须使用组合的思想来创建使用结构的新对象。所以,可以说Golang不支持继承。

例子1

显示类中继承性的Golang程序

下面的代码显示了我们如何在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包,它允许我们在屏幕上打印任何东西。
  • 然后,我们需要定义一个结构。这将是等同于父类的父结构。此外,我们还为这个结构定义了一个函数,它将以字符串形式返回锦标赛的名称。

  • 此外,我们还创建了两个新的结构,分别名为板球和曲棍球,这些结构继承了父结构games的所有属性,将成为子结构或类。

  • 调用main函数,这是我们程序的起点,代码从这里开始执行。

  • 创建板球结构的一个实例,并定义其中存在的新字符串。注意,名为new的字符串最初不是在这个结构中定义的,而是在游戏结构中定义的,我们能够使用该结构的属性的唯一原因是我们在板球结构中继承了其属性。

  • 现在调用定义在游戏结构中的AllGames()函数。

  • 将其返回的值存储在一个名为result的变量中,并使用fmt.Println()将其打印在屏幕上。

  • 重复上述步骤,创建曲棍球结构的实例并将结果打印到屏幕上。

例2

Golang程序显示一个类的多个继承关系

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的结构。这些将是父结构。此外,我们还为这些结构定义了一个函数,将返回一个字符串。

  • 然后,我们需要创建一个子结构,这个结构将继承父结构的所有属性。

  • 调用主函数。

  • 创建一个子结构的实例,并定义其中存在的父结构的属性。请注意,字符串最初不是在这个结构中定义的,而是在第一个和第二个结构中定义的,我们能够使用该结构的属性的唯一原因是我们在子结构中继承了其属性。

  • 现在调用printBase()函数。

  • 使用fmt.Println()将其打印在屏幕上。