Golang-interface
Basic introduction
In order, we should explain polymorphism, but before explaining polymorphism, we need to explain interface (interface), because in Golang polymorphism is mainly reflected through the interface.
Interface Quick Start
Such design requirements will also exist in Golang programming. I once said that a program is a world, and the existence of the real world will also appear in the program. We use the program to simulate the previous application scenario.
type Usb interface { Start () Stop () } type Phone struct { Name string } func (phone * Phone) Start () { fmt.Println ("Phone USB starts working") } func (phone * Phone) Stop () { fmt.Println ("Phone USB stops working") } type Camera struct { Name string } func (camera * Camera) Start () { fmt.Println ("Camera USB starts working") } func (camera * Camera) Stop () { fmt.Println ("Camera USB stopped working") }
func main() {
// First create the structure variable
computer := Computer{} phone := Phone{} camera := Camera{}
//key point
computer.Working(phone)
computer.Working(camera)
}
Explanation: The above code is a quick start case for interface programming.
Re-
interpretation of the concept of interface interface type can define a set of methods, but these do not need to be implemented. And interface cannot contain any variables. When a certain custom type (such as the structure Phone) is to be used, write these methods (implementation) according to the specific situation.
Basic grammar
Summary description:
1) All the methods in the interface have no method body, that is, the methods of the interface are not implemented methods. The interface embodies the ideas of programming polymorphism and high cohesion and low coupling.
2) The interface in Golang does not require an explicit implementation. As long as a variable contains all the methods in the interface type, then this variable implements this interface. Therefore, there is no keyword like implement in Golang
Application scenarios used by the interface
Notes and details
1) The interface itself cannot create an instance, but it can point to a variable (instance) of a custom type that implements the interface
2) All the methods in the interface have no method body, that is, all methods are not implemented.
3) In Golang, a custom type needs to implement all methods of an interface. We say that this custom type implements the interface.
4) A custom type can only assign an instance (variable) of the custom type to the interface type if it implements an interface
5) As long as it is a custom data type, the interface can be implemented, not just the structure type.
6) A custom type can implement multiple interfaces
7) There cannot be any variables in the Golang interface
8) An interface (such as the A interface) can inherit multiple other interfaces (such as the B and C interfaces). At this time, if you want to implement the A interface, you must also implement all the methods of the B and C interfaces.
9) The interface type is a pointer (reference type) by default. If it is used without initializing the interface, it will output nil
10) The empty interface interface {} has no method, so all types implement the empty interface, that is, we can put any A variable is assigned to the empty interface.
Exercise
Interface best practice examples
package main import ( "fmt" "sort" "math / rand" ) // Declare a slice of Hero structure type heroSlice [] Hero // Implement the interface interface func (heroslice HeroSlice) Len () int { return len (heroslice ) } // What sort is the Less method func (heroslice HeroSlice) Less (i, j int) bool { return heroslice [i] .Age <heroslice [j] .Age } func (heroslice HeroSlice) Swap (i, j int ) { // temp: = heroslice [i] // heroslice [i] = heroslice [j] // heroslice [j] = temp heroslice [i], heroslice [j] = heroslice [j], heroslice [i] } func main () { var heroes HeroSlice for i := 0; i < 10; i++{ hero := Hero{ Name :fmt.Sprintf("英雄|%d",rand.Intn(100)), Age : rand.Intn(100), } heroes = append(heroes,hero) } fmt.Println("======排序前======") for _, v := range heroes { fmt.Println(v) } sort.Sort(heroes) fmt.Println("======排序后======") for _, v := range heroes { fmt.Println(v) } }
Implementation interface vs inheritance
Everyone hears that now, you may be confused about implementing interface and inheritance. This question, then what is the difference between them?
Code description
Summary of the above code
1) When the A structure inherits the B structure, then the A structure automatically inherits the fields and methods of the B structure, and can be used directly
2) When the A structure needs extended functions, and does not want to To destroy the inheritance relationship, you can implement an interface, so we can think that: implementing an interface is complementary to the inheritance mechanism.
Implementing an interface can be seen as a supplement to inherit
Interface and inheritance solve different problems
The value of inheritance mainly lies in: resolving code reuse and maintainability.
The value of the interface mainly lies in: designing, designing various specifications (methods), and allowing other custom types to implement these methods.
Interface is more flexible than inheritance Person Student BirdAble LittleMonkey
Interfaces are more flexible than inheritance. Inheritance is to satisfy the is-a relationship, while the interface only needs to satisfy the like-a relationship.
The interface implements code decoupling to a certain extent