Interface basic knowledge sharing

Interface basic knowledge sharing

One, the definition of the interface

type 接口名 interface {
    
    
      方法名1(参数列表)返回值列表
      方法名2(参数列表)返回值列表
}

Two, the image example of the interface

In real life, the Usb interface is a vivid example of the interface, such as mobile phones, cameras, USB flash drives, which are designed according to uniform specifications. Usb has different responses after plugging in the Usb interface.

package main 
import (
	"fmt"
)
type Usb interface{
    
    
	start() //开始工作
	stop()  //结束工作
}
type Computer struct{
    
    

} //定义电脑U盘插口
type phone struct{
    
    
	Name string
}
type U struct{
    
    
	Name string
} //定义手机以及U盘对象
func (this *phone) start(){
    
    
	fmt.Println("我是手机")
}
func (this *phone) stop(){
    
    
	fmt.Println("再见")
}
//手机已经实现了接口
func (this *U) start(){
    
    
	fmt.Println("我是U盘")
}
func (this *U) stop(){
    
    
	fmt.Println("再见")
}
//U盘已经实现了接口
func (this *Computer) Working(usb Usb){
    
    
	usb.start()
	usb.stop()
}
func main(){
    
    
   com:=&Computer{
    
    }
   u:=&U{
    
    }
   p:=&phone{
    
    }
   com.Working(u)     //模拟U盘插入
   com.Working(p)     //模拟手机插入
}

Three, the difference between interface and inheritance

Inheritance solves the code reusability, the interface is to increase the function, and does not destroy the inheritance

Four, matters needing attention

1. No interface method has a method body; it is a reference type;
2. If a type implements all methods of the interface, it means that the type implements the interface;
3) The interface cannot have any variables;
4) An interface can Inherit multiple interfaces, and realize the interface must implement all the methods of the interface;
5) The empty interface has no methods, any type implements the empty interface, and any variable can be assigned to the empty interface;

Five, the interface reflects polymorphism

1. Polymorphic array

   var usb [2] Usb
   usb[0]= Phone {
    
    "小米手机"}
   usb[1]= U {
    
    "金士顿U盘"}
   fmt.Println(usb)

There is a small bug here. When transferring, the structure is a value type, but if you need to modify the value of the structure, you need to use a pointer type for reference transfer.

2. Type assertion

1. Since the interface is a general type, I don’t know what type it is, so type assertions must be used

var b interface{
    
    }
a:=b.(float32)

2. Use type assertion to get type

func Juge(item...interface{
    
    }){
    
    
     for i,v:=range item{
    
    
		 switch v.(type){
    
    
		 case string:{
    
    
			 fmt.Printf("第%d参数的类型string 值是%v\n",i,v)
		 }
		 case int:{
    
    
			fmt.Printf("第%d参数的类型是int 值%v\n",i,v)
		}
		case student:{
    
    
			fmt.Printf("第%d参数的类型是student 值是%v\n",i,v)
		}
		case *student:{
    
    
			fmt.Printf("第%d参数的类型是*student 值是%v\n",i,v)
		}
		 }
	 }
}

item...interface{}Representing multi-parameter conduction in the code

If you read this article to help yourself, I hope everyone likes to support it, thank you

Guess you like

Origin blog.csdn.net/yyq1102394156/article/details/114068453