Golang 类中方法的覆盖

Golang 类中方法的覆盖

当在Go中覆盖方法时,会创建一个与现有方法具有相同名称和接收者类型的新方法,并在现有方法的位置使用它。因此,Golang可以提供多态性,允许根据接收者的类型使用相同方法的不同实现。让我们通过示例来查看执行情况。

方法1:使用shape结构体

这里,shape结构体将包含一个area字段和一个返回area字段值的area方法。Rectangle和Square都继承了area()方法。输出将是它们的面积在控制台上打印出来。

步骤

  • 步骤1 - 创建一个main包,并在程序中声明fmt(格式包)包

  • 步骤2 - 首先,我们创建了一个带有名为area的float64类型的字段和名为Area()的方法的Shape结构体,该方法返回该字段的值。

  • 步骤3 - 然后,将Shape结构体嵌入到一个名为Rectangle的新结构中,该结构还具有名为width和height的两个float64类型的属性。

  • 步骤4 - 然后,我们为Rectangle结构创建一个名为CalculateArea()的函数,该函数使用宽度和高度变量计算面积,并将结果分配给从Shape结构体继承的area字段。

  • 步骤5 - Shape结构体也被嵌入到Square结构体中,它们都具有float64类型的字段side。

  • 步骤6 - 然后,使用side字段计算面积,并将结果分配给从Shape结构体继承的area字段,我们为Square结构体定义了方法CalculateArea()。

  • 步骤7 - 为了计算形状的面积,在main函数中构造一个Rectangle和Square结构的实例的指针,并调用它们各自的CalculateArea()函数。

  • 步骤8 - 我们在Rectangle和Square指针上调用Area()函数,以在计算它们的面积后获得形状的面积。

  • 步骤9 - 使用fmt.Println()函数在控制台上打印矩形的面积和正方形的面积,其中ln表示新行。

示例

在这个示例中,我们将学习如何使用shape结构体覆盖类中的方法。

package main
import (
    "fmt"
)

type Shape struct {
    area float64   //create area field in the struct
}

func (sq *Shape) Area() float64 {
    return sq.area   //return area of square
}

type Rectangle struct {
    Shape
    width  float64   //width of rectangle
    height float64   //height of rectangle
}

func (rect *Rectangle) CalculateArea() {
    rect.area = rect.width * rect.height     //area of rectangle
}

type Square struct {
    Shape
    side float64      //side of square
}

func (sq *Square) CalculateArea() {
    sq.area = sq.side * sq.side    //area of square
}

func main() {
    rect := &Rectangle{width: 16, height: 6}  //set the width and height of square
    rect.CalculateArea()
    fmt.Println("Area of rectangle: ", rect.Area())  //print area of rectangle 

    sq := □{side: 8} //set side of square
    sq.CalculateArea()
    fmt.Println("Area of square: ", sq.Area()) //print area of square.
}

输出

Area of rectangle:  96
Area of square:  64

方法2:使用形状接口

在这里,矩形和正方形通过实现面积方法来实现形状接口。最后,输出将是正方形和矩形的面积。

步骤

  • 步骤1 - 创建一个名为main的包,并在程序中声明fmt(格式包)包。

  • 步骤2 - 首先,我们创建一个称为Shape的接口,其中包含一个返回float64值的函数Area()。

  • 步骤3 - 然后,我们定义一个名为Rectangle的结构体,它有两个名为width和height的float64类型属性。

  • 步骤4 - 然后,通过创建一个与Shape接口具有相同签名的函数,并使用宽度和高度参数计算面积,来实现Rectangle结构体的Area()方法。

  • 步骤5 - 类似地,我们创建一个名为Square的结构体,其中有一个名为side的float64类型字段。

  • 步骤6 - 然后,我们使用side字段来确定面积,实现Square结构体的Area()方法。

  • 步骤7 - 在主函数中调用Rectangle结构体的Area()方法,并返回矩形的面积。

  • 步骤8 - 此外,我们构造一个Square结构体的实例,并使用其Area()方法获得正方形的面积。

  • 步骤9 - 使用fmt.Println()函数在控制台上打印出两个形状的面积,其中ln表示换行。

示例

在这个示例中,我们将学习如何使用形状接口来覆盖类中的方法。

package main
import (
    "fmt"
)

type Shape interface {
    Area() float64   //create area method
}

type Rectangle struct {
    width  float64   //width of rectangle
    height float64  //height of rectangle
}

func (rect Rectangle) Area() float64 {
    return rect.width * rect.height   //area of rectangle 
}

type Square struct {
    side float64  //side of square
}

func (sq Square) Area() float64 {
    return sq.side * sq.side    //area of square
}

func main() {
    rect := Rectangle{width: 16, height: 6}   //set the width and height of rectangle
    fmt.Println("Area of rectangle: ", rect.Area()) //print area of rectangle

    sq := Square{side: 8}  //set the sides of square
    fmt.Println("Area of square: ", sq.Area())  //print area of square
}

输出

Area of rectangle:  96
Area of square:  64

结论

我们执行了两个示例,展示了如何在类中重写方法。在第一个示例中,我们使用了形状结构体,在第二个示例中我们使用了形状接口。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程