Home >Backend Development >Golang >An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes

An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes

王林
王林Original
2024-04-20 09:21:011121browse

Both functional interfaces and abstract classes are used for code reusability, but they are implemented in different ways: functional interfaces through reference functions, and abstract classes through inheritance. Functional interfaces cannot be instantiated, but abstract classes can. Functional interfaces must implement all declared methods, while abstract classes can only implement some methods.

深入探讨 Golang 函数接口与抽象类的异同

Similarities and differences between Go functional interfaces and abstract classes

In the Go language, functional interfaces and abstract classes are two important Concepts, both of which are used to represent behavior and provide code reusability. However, the two differ in implementation and usage scenarios.

Functional Interface

A functional interface is a type that references a function with a specific signature. It defines the input and output parameters of the function, but does not need to implement the function body.

Syntax:

type fnType func(parameters) (returnType)

Example:

type Handler func(w http.ResponseWriter, r *http.Request)

Abstract class

An abstract class is a class that only contains declarations but no implementation. It defines an interface that requires subclasses to implement these declarations.

Grammar:

type Interface interface {
    Method1()
    Method2()
}

Similarities and Differences

Similarities:

  • Both provide code reusability, allowing the creation of replaceable components.
  • can be implemented by different types.

Differences:

  • Implementation method: Functional interfaces are implemented by referencing functions, while abstract classes are implemented through inheritance accomplish.
  • Instantiability: Functional interfaces cannot be instantiated, but abstract classes can.
  • Implementation choice: Functional interface must implement all declared methods, while abstract class can only implement some methods.
  • Syntax: Functional interfaces use the func keyword, while abstract classes use the interface keyword.

Practical case

Functional interface:

Functional interfaces can be used to create loosely coupled code, allowing Different components use different implementations.

type Shape interface {
    Area() float64
}

type Square struct {
    Side float64
}

func (s *Square) Area() float64 {
    return s.Side * s.Side
}

type Circle struct {
    Radius float64
}

func (c *Circle) Area() float64 {
    return math.Pi * c.Radius * c.Radius
}

func CalculateArea(shapes []Shape) float64 {
    totalArea := 0.0
    for _, shape := range shapes {
        totalArea += shape.Area()
    }
    return totalArea
}

Abstract class:

You can use abstract classes to define public behaviors and allow subclasses to implement or override these behaviors as needed.

type Animal interface {
    Speak() string
}

type Dog struct{}

func (d Dog) Speak() string {
    return "Woof!"
}

type Cat struct{}

func (c Cat) Speak() string {
    return "Meow!"
}

The above is the detailed content of An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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