Golang程序 显示类中超级关键字的使用

Golang程序 显示类中超级关键字的使用

在golang中,”super “这个词在Go中并不存在。在面向对象的编程语言中,”super “关键字用来表示父类或被继承的类。它经常被用来访问父类的方法或子类所修改的属性。在这篇文章中,我们将通过不同的例子来看看如何在类中使用super关键字。

方法1:使用子结构和父结构

在本方法中,我们将学习如何使用子结构和父结构在类中使用超级关键字。在这里,父结构被嵌入到子结构中。此外,子结构包含一个PrintName函数,该函数使用 “child.Parent.PrintName() “行,在打印自己的名称后调用父结构的PrintName方法。让我们来看看执行情况。

算法

  • 第1步– 创建一个包main,并在程序中声明fmt(格式包)包,其中main产生可执行的Example:s,fmt帮助格式化输入和输出。

  • 第2步– 创建一个名为 “Parent “的结构,给它一个名为 “name “的字符串型字段。

  • 第 3步– 在Parent结构上创建一个名为 “PrintName “的方法,接收器是一个指向该结构的指针。该过程将打印 “name “字段的值。

  • 第4步– 创建一个嵌入父结构的 “子 “结构。在子结构中还存在一个int类型的字段,名为 “age”。

  • 第5步– 在子结构上创建一个名为 “PrintName “的方法,接收器是指向该结构的一个指针。该过程首先打印子结构的 “name “字段的值。然后,”child.Parent.PrintName() “行被用来调用嵌入式父结构的 “PrintName “函数。

  • 第6步– 创建一个指向子结构的指针,并在 “main “方法中用数值填充其字段。

  • 第7步– 要使用 “super “关键字,在子结构的指针上调用 “PrintName “函数。

  • 第8步– 输出将是使用fmt.Println()函数在控制台打印的子结构和父结构的名称,其中ln表示新行。

例子

在这个例子中,我们将使用子结构和父结构来展示Golang中类中超级关键字的使用。

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:使用矩形结构和方形结构

在本例中,我们将看到如何使用矩形结构和方形结构来使用超级关键字。这里,我们开发了一个带有 “面积 “方法的 “形状 “接口。这个接口由 “矩形 “结构实现,它有一个具有相同签名的方法。正方形 “结构除了嵌入 “矩形 “类型外,还有一个 “面积 “函数。”sq.Rectangle.Area() “一行用于从 “Square “结构的 “Area “方法中调用嵌入的 “Rectangle “结构的 “Area “方法。让我们来看看执行情况。

算法

  • 第1步– 创建一个包main,并在程序中声明fmt(格式包)包,其中main产生可执行的Example:s,fmt帮助格式化输入和输出。

  • 第2步– 建立一个接口 Area的方法–signed shape()

  • 第3步– 创建矩形结构,有两个字段–宽度和高度,并应使用一个方法来实现该接口。Area(),它决定了一个矩形的面积。

  • 第4步 – 添加一个新的方形结构,将矩形嵌入其中。

  • 第 5步– 实现Square结构的Area()方法,它使用Rectangle来计算正方形的面积。

  • 第6步– 在主方法中创建一个指向Square结构的指针,并在其字段中填充数值。

  • 第7步– 在Square结构的引用上调用Area()函数,以调用Square结构的Area方法并打印正方形的面积。

  • 第8步– 在矩形结构指针上也调用Area()函数,打印出矩形的面积,并调用矩形结构的Area方法。

  • 第9步 – 比较两个函数的输出结果将显示差异,并使用fmt.Println()函数将其打印在屏幕上,ln表示新行。

例子

在这个例子中,我们将使用矩形结构和方形结构来展示Golang中类中超级关键字的使用。

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 := □{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

总结

我们用两个不同的例子执行了在类中使用超级关键字的程序。在第一个例子中,我们使用了子结构和父结构;在第二个例子中,我们使用了形状接口,并使用矩形和方形结构来实现它。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程