Home > Backend Development > Golang > Golang implements polymorphism

Golang implements polymorphism

WBOY
Release: 2023-05-06 10:29:06
Original
1669 people have browsed it

Golang is a programming language that supports object-oriented programming. Although it does not have concepts such as classes, inheritance, and polymorphism in traditional object-oriented programming languages, there are many methods in Golang to achieve polymorphism. This article will introduce how to implement polymorphism in Golang.

1. Interface

In Golang, interface is a way to achieve polymorphism. An interface is a collection of methods. A type is considered to implement the interface as long as it implements all methods in the interface. This method is more flexible and less coupled to the code than the traditional inheritance method.

The following is an example of implementing an interface:

type Animal interface {
    Move()
    Speak()
}

type Dog struct {
    Name string
}

func (d Dog) Move() {
    fmt.Printf("%s is moving\n", d.Name)
}

func (d Dog) Speak() {
    fmt.Printf("%s is speaking\n", d.Name)
}

func main() {
    var a Animal
    a = Dog{Name: "Tom"}
    a.Move()
    a.Speak()
}
Copy after login

In the above code, an interface Animal is defined, which contains two methods Move and Speak. Then a structure Dog is defined, which implements two methods in the Animal interface. In the main function, a variable a of Animal type is defined and assigned to an instance of Dog type. Then call the Move and Speak methods of a. Because Dog implements the Animal interface, it can be called directly.

It should be noted here that the interface in Golang is implemented implicitly, that is, as long as the type implements all methods in the interface, the interface is automatically implemented without explicit declaration.

2. Structure nesting

Structure nesting is also a way to achieve polymorphism. By nesting one type within another type, you can interface the methods of the nested type and define a common interface method in the outer type, thus achieving polymorphism.

The following is an example of structure nesting:

type Animal struct {
    Name string
}

func (a Animal) Move() {
    fmt.Printf("%s is moving\n", a.Name)
}

type Dog struct {
    Animal
}

func (d Dog) Speak() {
    fmt.Printf("%s is speaking\n", d.Name)
}

type Cat struct {
    Animal
}

func (c Cat) Speak() {
    fmt.Printf("%s is speaking\n", c.Name)
}

type Moveable interface {
    Move()
}

type Speakable interface {
    Speak()
}

func main() {
    var m Moveable
    var s Speakable
    m = Dog{Animal{Name: "Tom"}}
    s = Cat{Animal{Name: "Kitty"}}
    m.Move()
    s.Speak()
}
Copy after login

In the above code, an Animal structure is defined, and then the Dog and Cat structures are defined respectively, and they are all nested. In the Animal structure, and implement their respective methods. Then a Moveable interface and a Speakable interface are defined, and variables m and s containing these two interfaces are declared in the main function and assigned to instances of Dog and Cat types respectively. Then call the Move method of m and the Speak method of s respectively to output the corresponding information.

Through structure nesting, we can implement a common interface type to achieve polymorphism. It should be noted that nested structures cannot have methods or fields with the same name, otherwise conflicts will occur.

3. Switch statement

In addition to interface and structure nesting, Golang can also use switch statements to achieve polymorphism.

The following is an example of using the switch statement to achieve polymorphism:

type Animal struct {
    Name string
    Type string
}

func (a Animal) Move() {
    fmt.Printf("%s is moving\n", a.Name)
}

func (a Animal) Speak() {
    switch a.Type {
    case "dog":
        fmt.Printf("%s is barking\n", a.Name)
    case "cat":
        fmt.Printf("%s is meowing\n", a.Name)
    }
}

func main() {
    d := Animal{Name: "Tom", Type: "dog"}
    c := Animal{Name: "Kitty", Type: "cat"}
    d.Move()
    d.Speak()
    c.Move()
    c.Speak()
}
Copy after login

In the above code, an Animal structure is defined, and the Move and Speak methods are defined in the structure . The switch statement is used in the Speak method to output different information according to the Type attribute of Animal. In the main function, a dog type Animal instance and a cat type Animal instance are defined, and their Move and Speak methods are called respectively. Because the information output by the Speak method is different depending on the Type attribute, polymorphism is implemented.

It should be noted that when using the switch statement to implement polymorphism, it needs to be judged according to the type and output accordingly. If there are more types, the complexity of the code may be higher.

Summary:

This article introduces three ways to implement polymorphism in Golang: interface, structure nesting and switch statement. Different methods have their own advantages and applicable scenarios. Developers can choose the appropriate method to implement polymorphism according to specific needs.

The above is the detailed content of Golang implements polymorphism. 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