Go语言程序演示类中方法的重写
在Go语言中重写方法时,会创建一个与现有方法具有相同名称和接收器类型的新方法,并用它替换现有方法。因此,Go语言可以提供多态性,允许根据接收器的类型使用同一方法的不同实现。让我们通过示例来看一下执行过程。
方法一:使用shape结构体
在这里,shape结构体将包含一个area字段和一个area方法,该方法将返回area字段的值。Rectangle和Square都继承了area()方法。输出将是它们的面积打印在控制台上。
算法
步骤1 − 创建一个名为main的包,并在程序中声明fmt(格式化包)。
步骤2 − 首先,我们创建一个名为Shape的结构体,它包含一个名为area的float64类型字段和一个名为Area()的方法,该方法返回该字段的值。
步骤3 − 然后,Shape结构体被嵌入到一个名为Rectangle的新结构体中,该结构体还具有两个名为width和height的float64类型属性。
步骤4 − 然后,我们为Rectangle结构体创建一个名为CalculateArea()的函数,该函数使用width和height变量计算面积,并将结果赋值给从Shape结构体继承的area字段。
步骤5 − Shape结构体也被嵌入到Square结构体中,该结构体类似地包含一个名为side的float64类型字段。
步骤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 := &Square{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
方法二:使用shape接口
在这里,shape矩形和正方形通过实现area方法来实现shape接口。最后,输出将是正方形和矩形的面积。
算法
步骤1 − 创建一个名为main的包,并在程序中声明fmt(格式化包)。
步骤2 − 在开始时,我们创建一个名为Shape的接口,它包含一个名为Area()的函数,该函数返回一个float64值。
步骤3 − 然后,我们定义一个名为Rectangle的结构体,它具有两个名为width和height的float64类型属性。
步骤4 − 然后,通过创建一个与Shape接口签名相同的函数并使用width和height参数计算面积,来实现Rectangle结构体的Area()方法。
步骤5 − 以类似的方式,我们创建一个名为Square的结构体,它包含一个float64类型的字段。
步骤6 − 然后,这次使用side字段来确定面积,我们实现了Square结构体的Area()方法。
步骤7 − 在main函数中调用Rectangle结构体的Area()方法,它返回矩形的面积。
步骤8 − 此外,我们构造Square结构体的实例,并使用其Area()方法来获取正方形的面积。
步骤9 − 使用fmt.Println()函数(其中ln表示换行)将两个图形的面积打印到控制台上。
示例
在这个例子中,我们将学习如何使用shape接口在类中重写方法。
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
结论
我们执行了两个示例程序,演示了如何在类中重写方法。在第一个示例中,我们使用了shape结构体,在第二个示例中,我们使用了shape接口。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP