How to implement inheritance in golang?

coldplay.xixi
Release: 2020-07-18 15:53:27
Original
8093 people have browsed it

Golang’s method of implementing inheritance: Go uses combination to express the semantics of inheritance. The code is [type Cat struct {p *Pet}func (c *Cat) Catch(){fmt.Println("** ***")}].

How to implement inheritance in golang?

Golang’s method of implementing inheritance:

Use encapsulated topics and convert them into a programming language. Encapsulated as a pet, B encapsulated the dog as a pet, and when C needs a pet, both the cat and the dog are pets. The final result is that either the program still runs normally, or the program crashes unexpectedly, because it is very likely that a certain pet It may not meet specific needs.

This kind of repeated definition problem is attributed to different packaging standards. The packaging process of cats and dogs is carried out independently, and they are not discussed together to see if they can continue to abstract. Without a universal model, chaotic encapsulation leads to the fact that cats are pets and dogs are also pets.

This kind of encapsulation of multiple related concepts is prone to such problems, so appropriate unified analysis is performed to continue to abstract higher-level concepts. The concept of encapsulation is particularly important. Based on this, the original encapsulation can be liberated from this general concept and only retain its own characteristics, greatly simplifying the semantics of the model.

The concept of ordinary encapsulation and higher The relationship between the abstract encapsulation concepts of levels is inheritance in object-oriented, that is, cats inherit from pets, which means that cats not only have the characteristics of pets but also the highlights of cats themselves.

The same is true for dogs, dogs are pets, dogs It is also the dog itself, reflecting its own characteristics.

The Go language is different from other mainstream object-oriented languages. Go does not support inheritance features, so there is no single inheritance, multiple Complex concepts such as inheritance and overriding methods.

How does Go describe the relationship between ordinary encapsulation and abstract encapsulation?

It certainly does not define cats as pets, dogs are also defined The way to become a pet!

Go implements the semantics of inheritance not through the extends keyword but through structure combination. Please see the relevant code.

Pets should be able to be civilized and martial. We don’t care about the fields of the structure here, so no related fields are defined.

type Pet struct {
}
func (p *Pet) Skill() {
    fmt.Println("能文能武的宠物")
}
Copy after login

Cats are pets that can catch mice, and Go uses a combination of methods to express the semantics of inheritance.

type Cat struct {
    p *Pet
}
func (c *Cat) Catch() {
    fmt.Println("老鼠天敌喵喵喵")
}
Copy after login

Dogs are natural A pet with navigation function, look at the superpower of my guide dog!

type Dog struct {
    p *Pet
}
func (d *Dog) Navigate() {
    fmt.Println("自带导航汪汪汪")
}
Copy after login

Next, C began to test whether cats and dogs as pets have the basic requirements of being able to be civilized and martial, and at the same time, whether they have their own Features?

func TestExtendInstance(t *testing.T) {
    p := new(Pet)
    d := new(Dog)
    d.p = p
    // 自带导航汪汪汪
    d.Navigate()
    // 能文能武的宠物
    d.p.Skill()
    fmt.Println()
    c := new(Cat)
    c.p = p
    // 老鼠天敌喵喵喵
    c.Catch()
    // 能文能武的宠物
    c.p.Skill()
}
Copy after login

The above results prove that although the Go language does not support the inheritance feature expressed by the extends keyword, inheritance semantics can also be achieved by using a combination method

Related learning recommendations: Go language tutorial

The above is the detailed content of How to implement inheritance in golang?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!