Interface theory
- An interface is a collection of one or more method signatures
- As long as a type has all the method signatures for that interface, that interface is implemented, without showing which interface the declaration implements, which is called Structural Typing
- Interfaces are only method declarations, no implementations, no data fields
- Interfaces can be embedded anonymously in other interfaces, or embedded in a structure
- When an object is assigned to an interface, a copy occurs, and the interface stores a pointer to the replica that cannot modify the state of the replica or retrieve the pointer
- The interface is equal to nil only if the interface stores both the type and the object are nil
- Interface calls do not automatically convert receiver
- Interface also supports anonymous field methods
- Interfaces can also be implemented to resemble the polymorphism in OOP
- An empty interface can be used as a container for task type data
Example 1, creating an interface
// interface 接口 练习//实现接口的原则就是//实现了它定义的方法,就默认是实现了接口package mainimport "fmt"//声明一个接口type USB interface { //声明方法Name, 并设置 返回值类型string Name() string //声明方法Connect方法,无返回值 Connect()}//声明一个类型,在Go语言中,对应的就是struct类型type PhoneConnector struct { //声明一个私有属性 name string}//---------------------声明完Name,Connector方法后,就是实现了USB接口了---------------------------//使用receiver,将类型跟方法进行绑定func (pc PhoneConnector)Name() string{ return pc.name}func (pc PhoneConnector)Connector() { fmt.Println("connected:\t", pc.name)}//--------------------------------------------------------------------------------------func main() { a := PhoneConnector{"apple"} fmt.Println("Name:\t", a.Name()) a.Connector()}
2. Nesting exercises between interfaces
Interface nested Practice Test Package Mainimport "FMT"//define an empty interface//In this case, all classes, the default implementation of this interface, because it does not have a method//definition of the Null interface, it is equivalent to define an object, the highest level/ /is its subclass, there is no limit to the type Nullempty interface {}//defines a parent interface type HOME Interface {//This interface, only one method Name () string}//is defined Port, which is equivalent to a sub-interface of type MyHome interface {Show () string//So nested an interface home}//declares a struct type beijinghome struct {name string}//-----------Start Creating Method-----Func (Info beijinghome) Show () {fmt. Println ("Show ()--->info.name:\t", info.name)}func (info beijinghome) name () string{return Info.name}func main () { A: = beijinghome{"Yihuyuan"} fmt. Println ("Name:\t", A.name ()) a.show ()//------The following is a demonstration of the problem of---reproductions mentioned in the above theory----b: = a b.name = "Lenovo"//modified and not modified The value of a in B.show () Disconnect (a) Disconnect2 (a)}//design a simple Type assertion func Disconnect (home home) {if pc, OK: = home. Beijinghome); ok{FMT. Println ("Disconnected:\t", Pc.name) return} FMT. Println ("Unknown decive.")} Design a simple type assertion//incoming parameter, yes, NULL interface//Actually, for incoming parameters, there is no limit to the FuncDisconnect2 (Home interface{}) {///to the Disconnect () method, the transformation,//Because, the incoming parameter is the top level, equivalent to the Java object, there is no restriction//type, you need to judge the switch V: = home. (type) {Case Beijinghome:fmt. Println ("Disconnected:\t", V.name) default:fmt. Println ("Unknown decive.") }}
3. Conversion between different interfaces
//不同接口之间的转换,类似于Java中的向上转型,或者向下转型//就是说,有两个接口A, B//其中,A接口里,嵌入了B//那么A接口可以转换成B接口,但是,B接口不能转换成A接口,因为//A接口里,可能包含B接口里没有的方法//也就是说,多的可以向少的转换,反之不可。package mainimport "fmt"type USB2 interface { Name() string Connecter}type Connecter interface { Connect()}type PcConnecter struct { name string}func (pcConnecter PcConnecter)Name() string { return pcConnecter.name}func(pcConnecter PcConnecter)Connect() { fmt.Println("Connected:\t", pcConnecter.name)}func main() { pc := PcConnecter{"appleConnector"} var a Connecter //将USE2 类型,强制 转换成了Connecter类型 a = Connecter(pc) a.Connect() //------验证----只有当接口存储的类型和对象都为nil时,接口才等于nil //声明一个空接口, 也没有赋值,就是nil var aa interface{} fmt.Println( aa == nil) // aa 本身就是一个nil,啥也没存 //变量p, 是一个指向int类型的指针 //直接初始化为nil var p *int = nil aa = p //aa 指向p fmt.Println( aa == nil)}
The interface of Go language