Golang implements interfaces and inheritance

The little monkey inherits the old monkey, so the abilities and methods owned by the old monkey will be automatically inherited by the old monkey. The little monkey can get the fields and methods of the old monkey without doing any processing, because it is an inheritance relationship.

But the little monkey also has other skills, such as flying like a bird and hoping to swim like a fish. In this way, the ability of the little monkey can be enhanced through the implementation of the relationship and the implementation of the interface.

When a structure inherits another structure, then the structure has the fields and methods of the inherited structure. If this structure wants to extend some of its methods without destroying the inheritance relationship, then it can be implemented.

Interfaces are a complement to inheritance. The new structure after inheritance implements the interface and does not affect the old structure. (In fact, it is to extend the functions of the new structure by implementing the interface, which does not affect the old structure and its inheritance relationship)

Expand the function of the structure without destroying the inheritance relationship. In fact, it is a decoupling method to expand the function of the structure.

package main
import "fmt"

type Monkey struct {
	Name string
}

// 给monkey结构体绑定了一个方法叫做climbing
func (m *Monkey) climbing() {
	fmt.Println(m.Name, "生来会爬树")
}

type Bird interface {
	Flying()
}

type Fish interface {
	Swimming()
}

type LittleMonkey struct {
	Monkey //继承关系
}

func (l *LittleMonkey) Flying() {
	fmt.Println(l.Name, "会飞")
}

func (l *LittleMonkey) Swimming() {
	fmt.Println(l.Name, "会游泳")
}

func main() {
	lm := &LittleMonkey{
		Monkey{
			Name: "悟空",
		},
	}

	lm.climbing()
	lm.Flying()
	lm.Swimming()
}

悟空 生来会爬树
悟空 会飞
悟空 会游泳

Summary of the above code:

1) When structure A inherits structure B, structure A automatically inherits the fields and methods of structure B and can be used directly.

2) When structure A needs to extend its functions and does not want to destroy the inheritance relationship, it can implement an interface. Therefore, we can think that implementing interfaces is a supplement to the inheritance mechanism!

English is a skill now required of football players and college students.

The first way is to put the language learning skill into the athlete structure. But basketball players don’t need to master this English skill. Therefore, it is unreasonable to write the method directly into the athlete structure.

Therefore, we need to define an interface here and let the structure that needs to implement the interface implement it. This will not affect other structures and decouple it.

This does not destroy the inheritance relationship and at the same time ensures normativeness.

Implementing interfaces vs inheritance

>Interfaces and inheritance solve different problems

The value of inheritance mainly lies in: solving code reusability (allowing a structure to inherit another structure, so that repeated code does not need to be written) and maintainability (adding a method to the inherited structure, Other structures have fields and methods).

The value of interfaces mainly lies in: design, designing various specifications (methods), and allowing other custom types to implement these methods.

>Interfaces are more flexible than inheritance

Interfaces are more flexible than inheritance. Inheritance satisfies the is-a relationship, while interfaces only need to satisfy the like-a relationship.

Interfaces achieve code decoupling to a certain extent

Guess you like

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