Golang 展示在类中使用super关键字
在Golang中,”super”这个术语在Go语言中不存在。”super”关键字用于面向对象编程语言中表示父类或被继承的类。常常用于访问子类已修改的父类方法或属性。在本文中,我们将看到如何在类中使用super关键字,并给出不同的示例。
方法1:使用子结构体和父结构体
在这种方法中,我们将学习如何使用子结构体和父结构体在类中使用super关键字。在这里,一个父结构体被嵌入在子结构体中。此外,子结构体包含一个PrintName函数,在打印自己的名字后,使用”child.Parent.PrintName()”语句来调用父结构体的PrintName方法。让我们看看执行结果。
步骤
- 步骤1 - 创建一个main包,并在程序中声明fmt(格式化包)包,其中main生成可执行示例,并且fmt在格式化输入和输出方面有帮助。
-
步骤2 - 创建一个名为”Parent”的结构体,并给它一个名为”name”的字符串类型字段。
-
步骤3 - 在Parent结构体上创建一个名为”PrintName”的方法,接收者为指向该结构体的指针。该过程将打印出”name”字段的值。
-
步骤4 - 创建一个”Child”结构体,嵌入父结构体。Child结构体中还有一个类型为int的字段”age”。
-
步骤5 - 在Child结构体上创建一个名为”PrintName”的方法,接收者为指向该结构体的指针。该过程首先打印出Child结构体的”name”字段的值。然后使用”child.Parent.PrintName()”语句调用嵌入的Parent结构体的”PrintName”函数。
-
步骤6 - 创建一个指向Child结构体的指针,并在”main”方法中为其字段赋值。
-
步骤7 - 要使用”super”关键字,在Child结构体指针上调用”PrintName”函数。
-
步骤8 - 输出将通过使用fmt.Println()函数在控制台上打印出子类和父类的名字,其中ln表示换行。
实例
在这个示例中,我们将使用子结构体和父结构体来展示在Golang中类中使用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:使用rectangle struct和square struct
在这个示例中,我们将看到如何在rectangle struct和square struct中使用super关键字。这里创建了一个接口”Shape”,其中包含一个”Area”方法。这个接口被”Rectangle” struct实现,它有一个相同签名的方法。”Square” struct除了嵌入”Rectangle”类型外,还有一个”Area”函数。”sq.Rectangle.Area()”一行代码用于从”Square” struct的”Area”方法中调用嵌入的”Rectangle” struct的”Area”方法。让我们来看一下执行过程。
步骤
- 步骤1 - 创建一个main包并在程序中声明fmt(format package)包,其中main生成可执行的示例,fmt帮助格式化输入和输出。
-
步骤2 - 建立一个带有方法签名为shape()的Area接口。
-
步骤3 - 创建Rectangle struct,包含两个字段width和height,并使用一个方法Area()来实现接口。Area()方法用于计算矩形的面积。
-
步骤4 - 添加一个新的square结构来嵌入Rectangle。
-
步骤5 - 实现Square struct的Area()方法,该方法使用Rectangle来计算正方形的面积。
-
步骤6 - 在main方法中创建一个指向Square struct的指针,并填充其字段的值。
-
步骤7 - 在Square struct引用上调用Area()函数,以调用Square struct的Area方法并打印正方形的面积。
-
步骤8 - 也调用Rectangle struct指针上的Area()函数,打印矩形的面积并调用Rectangle struct的Area方法。
-
步骤9 - 比较这两个函数的输出将展示差异,并使用fmt.Println()函数在屏幕上打印出来,其中ln代表换行。
示例
在这个示例中,我们将使用rectangle struct和square struct来展示在Golang中类中使用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 := □{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关键字的程序。在第一个示例中,我们使用了子结构和父结构,在第二个示例中,我们使用了形状接口,并使用矩形和正方形结构实现了它。
极客笔记