Detailed explanation of how golang achieves inheritance effect

PHPz
Release: 2023-04-11 14:23:57
Original
826 people have browsed it

In the Go language, there is no concept of class, so there is no inheritance in traditional object-oriented languages. However, in the Go language, an inheritance-like effect can be achieved through the combination of structures and anonymous fields.

First, let us look at the usage of combination in go language. Composition refers to embedding one structure within another structure to achieve code reuse and modularization. For example, we can define a Person structure:

type Person struct {
    Name string
    Age int
}
Copy after login

Then, we can embed the Person structure in the Student structure:

type Student struct {
    Person
    School string
}
Copy after login

In this way, the Student structure not only has its own Properties, you can also access properties in the parent class through the Person property. For example:

s := Student{
    Person: Person{Name: "Tom", Age: 18},
    School: "CMS",
}
fmt.Println(s.Name) // Tom
fmt.Println(s.Age)  // 18
Copy after login

You can also access methods in the parent class through the embedded Person attribute:

func (p *Person) SayHello() {
    fmt.Printf("Hello, my name is %s.\n", p.Name)
}

type Student struct {
    Person
    School string
}

func main() {
    s := Student{
        Person: Person{Name: "Tom", Age: 18},
        School: "CMS",
    }
    s.SayHello() // Hello, my name is Tom.
}
Copy after login

However, if we want to define a method in the Student structure with the same name as the Person structure ,what can we do about it? At this time, you need to use the characteristics of anonymous fields.

Anonymous field is a special field type that does not specify a name, only a type. Through anonymous fields, variables and methods with the same name in the parent class can be referenced in the subclass. We can use the following method to define a method with the same name as the Person structure in the Student structure:

type Person struct {
    Name string
    Age int
}

func (p *Person) SayHello() {
    fmt.Printf("Hello, my name is %s.\n", p.Name)
}

type Student struct {
    Person
    School string
}

func (s *Student) SayHello() {
    fmt.Printf("Hello, my name is %s and I am a student of %s.\n", s.Name, s.School)
}

func main() {
    s := Student{
        Person: Person{Name: "Tom", Age: 18},
        School: "CMS",
    }
    s.SayHello() // Hello, my name is Tom and I am a student of CMS.
}
Copy after login

In the above code, we define the SayHello method with the same name as the Person structure in the Student structure, and The properties in the parent class are accessed through s.Name and s.School.

In summary, the Go language does not have the concept of inheritance, but through the combination of structures and anonymous fields, effects similar to inheritance can be achieved.

The above is the detailed content of Detailed explanation of how golang achieves inheritance effect. 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