Home > Backend Development > Golang > Abstract class implementation of golang function in object-oriented programming

Abstract class implementation of golang function in object-oriented programming

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2024-05-03 14:00:02
Original
590 people have browsed it

In Golang, abstract class functionality can be achieved by implementing an interface and defining a function: define the interface and declare the method signature. Define functions and implement interface methods. Instantiate the structure and call the function. In the actual case, the Shape interface and the corresponding specific shape function are used to draw different shapes.

Abstract class implementation of golang function in object-oriented programming

Abstract class implementation of Golang functions in object-oriented programming

In object-oriented programming (OOP), an abstract class is a class that cannot be instantiated class, but can be inherited by subclasses. Abstract classes usually contain abstract methods, which are functions that only declare a method signature but no implementation.

In Golang, abstract classes cannot be declared, but functions can be used to implement similar abstract functions. The specific method is as follows:

1. Define an interface:

type MyInterface interface {
    DoSomething()
}
Copy after login

2. Define a function:

func (f *MyStruct) DoSomething() {
    // 具体的实现
}
Copy after login

3. Implement the interface:

type MyStruct struct {
    f func()
}

func (s *MyStruct) DoSomething() {
    s.f()
}
Copy after login

4. Instantiate the structure and call the function:

s := &MyStruct{f: func() { fmt.Println("Do Something") }}
s.DoSomething() // 输出: Do Something
Copy after login

Practical case:

Suppose we have a drawing program that needs to draw multiple shapes, but the specific shape drawing logic is different. We can use functions to implement abstract classes to solve this problem:

1. Define the Shape interface:

type Shape interface {
    Draw()
}
Copy after login

2. Define the function of the specific shape:

func DrawCircle(x, y, radius float64) {
    // 绘制圆形
}

func DrawSquare(x, y, width float64) {
    // 绘制正方形
}

func DrawTriangle(x1, y1, x2, y2, x3, y3 float64) {
    // 绘制三角形
}
Copy after login

3. Implement the Shape interface:

type Circle struct {
    x, y, radius float64
}

func (c *Circle) Draw() {
    DrawCircle(c.x, c.y, c.radius)
}

type Square struct {
    x, y, width float64
}

func (s *Square) Draw() {
     DrawSquare(s.x, s.y, s.width)
}
Copy after login

4. Use specific shapes to draw graphics:

shapes := []Shape{
    &Circle{x: 10, y: 10, radius: 5},
    &Square{x: 20, y: 20, width: 10},
}

for _, shape := range shapes {
    shape.Draw()
}
Copy after login

The above is the detailed content of Abstract class implementation of golang function 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