In-depth discussion and details of Golang Struct inheritance

1) The structure can use all fields and methods of the nested anonymous structure, that is: fields and methods with the first letter in uppercase or lowercase can be used.

type A struct {
	Name string
	age  int
}

func (a *A) SayName() {
	fmt.Println("A say name", a.Name)
}

func (a *A) sayAge() {
	fmt.Println("A say age", a.age)
}

type B struct {
	sex string
	A
}

func main() {
	b := &B{
		sex: "male",
		A: A{
			Name: "lucas",
			age:  29,
		},
	}
	b.SayName()
	b.A.sayAge()
	fmt.Println(b.Name)
	fmt.Println(b.age)
}

2) Anonymous structure field access can be simplified, as shown in the figure

Summary of the above code

(1) When we access a field or method directly through b, the execution process is as follows, such as b.Name

(2) The compiler will first check whether the type corresponding to b has a Name. If so, it will directly call the Name field of type B.

(3) If not, check to see if the anonymous structure A embedded in B has a Name field declared. If so, call it. If not, continue to search... If it cannot be found, an error will be reported ( if There is also an embedded structure in A, so it can be called )

(4) When a structure and an anonymous structure have the same fields or methods, the compiler uses the nearest access principle to access them. If you want to access the fields and methods of the anonymous structure, you can distinguish them by the name of the anonymous structure.

type A struct {
	Name string
	age  int
}

func (a *A) SayName() {
	fmt.Println("A say name", a.Name)
}

func (a *A) sayAge() {
	fmt.Println("A say age", a.age)
}

type B struct {
	sex  string
	Name string
	A
}

func (b *B) SayName() {
	fmt.Println("B say name", b.Name)
}

func main() {
	b := &B{
		sex:  "male",
		Name: "jerry",
		A: A{
			Name: "lucas",
			age:  29,
		},
	}
	b.SayName()
	fmt.Println(b.Name)
}


B say name jerry
jerry

You can see that B has already found its own field Name when looking for fields, so it will not look for the Name field in A.

(5) The structure embeds two (or more) anonymous structures. If the two anonymous structures have the same fields and methods (and the structure itself does not have fields and methods with the same name), they must be explicitly specified when accessing. Anonymous structure name, otherwise a compilation error will be reported.

type A struct {
	Name string
	age  int
}

func (a *A) SayName() {
	fmt.Println("A say name", a.Name)
}

type B struct {
	Name string
	sex  string
}

func (b *B) SayName() {
	fmt.Println("B say name", b.Name)
}

type C struct {
	A
	B
}

func main() {
	c := &C{
		A: A{
			Name: "lucas",
			age: 23,
		},
		B: B{
			Name: "jerry",
			sex: "male",
		},
	}
	
	c.A.SayName()
	fmt.Println(c.A.Name)
}

(6) If a struct nests a named structure, this pattern is a combination. If it is a combination relationship, then the name of the structure must be brought when accessing the fields or methods of the combined structure.

(7) After nesting anonymous structures, you can also directly specify the value of each anonymous structure field when creating a structure variable (instance).

  

 

Multiple inheritance explained


If a struct nests multiple anonymous structures , then the structure can directly access the fields and methods of the nested anonymous structures, thus achieving multiple inheritance.

In TV, you can use the properties and methods in Goods, and you can also use the properties and methods in Brand. 

Multiple inheritance details

1) If the embedded anonymous structure has the same field name or method name, it needs to be distinguished by the anonymous structure type name when accessing.

2) In order to ensure the simplicity of the code, it is recommended that you try not to use multiple inheritance.

Guess you like

Origin blog.csdn.net/qq_34556414/article/details/134044305