Home > Backend Development > Golang > Implementation of polymorphism of golang functions in object-oriented programming

Implementation of polymorphism of golang functions in object-oriented programming

PHPz
Release: 2024-05-04 16:03:02
Original
813 people have browsed it

In the Go language, functions can be stored as interface values ​​to achieve polymorphism: define an interface and specify a set of method signatures. Create a type that implements the interface and implements these methods for it. Define a function that accepts interface values ​​as input. Methods that only use the interface value in the function, regardless of the actual type. Call functions with values ​​of different types to achieve polymorphism.

Implementation of polymorphism of golang functions in object-oriented programming

Polymorphism implementation of Go language functions in object-oriented programming

In Go language, functions can be stored as interface values, which allows us to Implement polymorphism even though the Go language itself is not object-oriented.

Interface

The interface defines the signature of a set of methods. Types that implement the interface must implement these methods. For example, we define a Shape interface:

type Shape interface {
    Area() float64
}
Copy after login

type implements the interface

We can create a type that implements the Shape interface as follows:

type Rectangle struct {
    width, height float64
}

func (r Rectangle) Area() float64 {
    return r.width * r.height
}

type Circle struct {
    radius float64
}

func (c Circle) Area() float64 {
    return math.Pi * c.radius * c.radius
}
Copy after login

Using functions as interface values

Now that we have created a Shape interface, we can use functions as its values. To do this, we define a calculateArea function that accepts a Shape interface value as input.

func calculateArea(s Shape) float64 {
    return s.Area()
}
Copy after login

Practical Case

Now, let us create an example to demonstrate how polymorphism is implemented.

func main() {
    // 创建一个 Rectangle 和一个 Circle
    r := Rectangle{width: 2, height: 3}
    c := Circle{radius: 5}

    // 使用函数计算他们的面积
    areaR := calculateArea(r)
    areaC := calculateArea(c)

    fmt.Println("Rectangle area:", areaR)
    fmt.Println("Circle area:", areaC)
}
Copy after login

Output:

Rectangle area: 6
Circle area: 78.53981633974483
Copy after login

In this example, the calculateArea function only uses the Area of the Shape interface passed in method, regardless of whether the actual type is Rectangle or Circle. This is the embodiment of polymorphism.

The above is the detailed content of Implementation of polymorphism of golang functions in object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template