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