go基础笔记-接口

基本介绍
在学习多态前,需要先了解接口(interface),因为在 Golang 中 多态特性主要是通过接口来体现的。

为什么有接口
usb插槽就是现实中的接口。
你可以把手机,相机,u盘都插在usb插槽上,而不用担心那个插槽是专门插哪个的,原因是做usb插槽的厂家和做各种设备的厂家都遵守了统一的规定包括尺寸,排线等等。
这样的设计需求在 Golang 编程中也是会大量存在的。

案例演示
package main
import (
    "fmt"
)
 
//声明/定义一个接口
type Usb interface {
    //声明了两个没有实现的方法
    Start() 
    Stop()
}
 
 
//声明/定义一个接口
type Usb2 interface {
    //声明了两个没有实现的方法
    Start() 
    Stop()
    Test()
}
 
 
type Phone struct {
 
}  
 
//让Phone 实现 Usb接口的方法
func (p Phone) Start() {
    fmt.Println("手机开始工作。。。")
}
func (p Phone) Stop() {
    fmt.Println("手机停止工作。。。")
}
 
type Camera struct {
 
}
//让Camera 实现   Usb接口的方法
func (c Camera) Start() {
    fmt.Println("相机开始工作。。。")
}
func (c Camera) Stop() {
    fmt.Println("相机停止工作。。。")
}
 
 
//计算机
type Computer struct {
 
}
 
//编写一个方法Working 方法,接收一个Usb接口类型变量
//只要是实现了 Usb接口 (所谓实现Usb接口,就是指实现了 Usb接口声明所有方法)
func (c Computer) Working(usb Usb) {
 
    //通过usb接口变量来调用Start和Stop方法
    usb.Start()
    usb.Stop()
}
 
func main() {
    //测试
    //先创建结构体变量
    computer := Computer{}
    phone := Phone{}
    camera := Camera{}
    //关键点
    computer.Working(phone)
    computer.Working(camera) //
}



接口概念的再说明
interface 类型可以定义一组方法,但是这些不需要实现。 
interface 不能包含任何变量。
到某个自定义类型(比如结构体 Phone)要使用的时候,在根据具体情况把这些方法写出来(实现)。


type 接口名interface{
    method1(参数列表)返回值列表
    method2(参数列表)返回值列表
    ...
}
实现接口所有方法
func(t自定义类型)method1(参数列表)返回值列表{
    //方法实现
}
func(t自定义类型)method1(参数列表)返回值列表{
    //方法实现
}
//...


说明:
接口里的所有方法都没有方法体,即接口的方法都是没有实现的方法。
接口体现了程序设计的多态和高内聚低偶合的思想。
Golang 中的接口,不需要显式的实现。
只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。因此,Golang 中没有 implement 这样的关键字


注意事项和使用细节
接口本身不能创建实例,但是可以指向一个实现了该接口的自定义类型的变量(实例)

type Stu struct {
    Name string
}
 
func (stu Stu) Say() {
    fmt.Println("Stu Say()")
}
 
type AInterface interface {
    Say()
}
func main() {
    var stu Stu //结构体变量,实现了 Say() 实现了 AInterface
     var a AInterface = stu
    a.Say()
}


接口中所有的方法都没有方法体,即都是没有实现的方法。
在 Golang 中,一个自定义类型需要将某个接口的所有方法都实现,我们说这个自定义类型实现了该接口。
一个自定义类型只有实现了某个接口,才能将该自定义类型的实例(变量)赋给接口类型
只要是自定义数据类型,就可以实现接口,不仅仅是结构体类型。


type integer int
 
func (i integer) Say() {
    fmt.Println("integer Say i =" ,i )
}
 
type AInterface interface {
    Say()
}
func main() {
        var i integer = 10
    var b AInterface = i
    b.Say() // integer Say i = 10
}


一个自定义类型可以实现多个接口
type AInterface interface {
    Say()
}
 
type BInterface interface {
    Hello()
}
type Monster struct {
 
}
func (m Monster) Hello() {
    fmt.Println("Monster Hello()~~")
}
 
func (m Monster) Say() {
    fmt.Println("Monster Say()~~")
}
 
func main() {
    //Monster实现了AInterface 和 BInterface
    var monster Monster
    var a2 AInterface = monster
    var b2 BInterface = monster
    a2.Say()
    b2.Hello()
}


Golang 接口中不能有任何变量

一个接口(比如 A 接口)可以继承多个别的接口(比如 B,C 接口),这时如果要实现 A 接口,也必须将 B,C 接口的方法也全部实现。
package main
import (
    "fmt"
)
 
type BInterface interface {
    test01()
}
 
type CInterface interface {
    test02()
}
 
type AInterface interface {
    BInterface
    CInterface
    test03()
}
 
//如果需要实现AInterface,就需要将BInterface CInterface的方法都实现
type Stu struct {
}
func (stu Stu) test01() {
 
}
func (stu Stu) test02() {
    
}
func (stu Stu) test03() {
    
}
 
func main() {
    var stu Stu
    var a AInterface = stu
    a.test01()
}
interface 类型默认是一个指针(引用类型),如果没有对 interface 初始化就使用,那么会输出 nil
空接口 interface{} 没有任何方法,所以所有类型都实现了空接口, 即我们可以把任何一个变量赋给空接口。
type T  interface{
 
}
 
func main() {
    var t T = stu //ok
    fmt.Println(t)
    var t2 interface{}  = stu
    var num1 float64 = 8.8
    t2 = num1
    t = num1
    fmt.Println(t2, t)
}





 

go基础笔记-接口
基本介绍在学习多态前,需要先了解接口(interface),因为在 Golang 中 多态特性主要是通过接口来体现的。
为什么有接口usb插槽就是现实中的接口。你可以把手机,相机,u盘都插在usb插槽上,而不用担心那个插槽是专门插哪个的,原因是做usb插槽的厂家和做各种设备的厂家都遵守了统一的规定包括尺寸,排线等等。这样的设计需求在 Golang 编程中也是会大量存在的。
案例演示package mainimport ("fmt") //声明/定义一个接口type Usb interface {//声明了两个没有实现的方法Start() Stop()}  //声明/定义一个接口type Usb2 interface {//声明了两个没有实现的方法Start() Stop()Test()}  type Phone struct { }   //让Phone 实现 Usb接口的方法func (p Phone) Start() {fmt.Println("手机开始工作。。。")}func (p Phone) Stop() {fmt.Println("手机停止工作。。。")} type Camera struct { }//让Camera 实现   Usb接口的方法func (c Camera) Start() {fmt.Println("相机开始工作。。。")}func (c Camera) Stop() {fmt.Println("相机停止工作。。。")}  //计算机type Computer struct { } //编写一个方法Working 方法,接收一个Usb接口类型变量//只要是实现了 Usb接口 (所谓实现Usb接口,就是指实现了 Usb接口声明所有方法)func (c Computer) Working(usb Usb) { //通过usb接口变量来调用Start和Stop方法usb.Start()usb.Stop()} func main() {//测试//先创建结构体变量computer := Computer{}phone := Phone{}camera := Camera{}//关键点computer.Working(phone)computer.Working(camera) //}


接口概念的再说明interface 类型可以定义一组方法,但是这些不需要实现。 interface 不能包含任何变量。到某个自定义类型(比如结构体 Phone)要使用的时候,在根据具体情况把这些方法写出来(实现)。

type 接口名interface{    method1(参数列表)返回值列表    method2(参数列表)返回值列表    ...}实现接口所有方法func(t自定义类型)method1(参数列表)返回值列表{    //方法实现}func(t自定义类型)method1(参数列表)返回值列表{    //方法实现}//...

说明:接口里的所有方法都没有方法体,即接口的方法都是没有实现的方法。接口体现了程序设计的多态和高内聚低偶合的思想。Golang 中的接口,不需要显式的实现。只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。因此,Golang 中没有 implement 这样的关键字

注意事项和使用细节接口本身不能创建实例,但是可以指向一个实现了该接口的自定义类型的变量(实例)
type Stu struct {Name string} func (stu Stu) Say() {fmt.Println("Stu Say()")} type AInterface interface {Say()}func main() {var stu Stu //结构体变量,实现了 Say() 实现了 AInterface var a AInterface = stua.Say()}

接口中所有的方法都没有方法体,即都是没有实现的方法。在 Golang 中,一个自定义类型需要将某个接口的所有方法都实现,我们说这个自定义类型实现了该接口。一个自定义类型只有实现了某个接口,才能将该自定义类型的实例(变量)赋给接口类型只要是自定义数据类型,就可以实现接口,不仅仅是结构体类型。

type integer int func (i integer) Say() {fmt.Println("integer Say i =" ,i )} type AInterface interface {Say()}func main() {        var i integer = 10var b AInterface = ib.Say() // integer Say i = 10}

一个自定义类型可以实现多个接口type AInterface interface {Say()} type BInterface interface {Hello()}type Monster struct { }func (m Monster) Hello() {fmt.Println("Monster Hello()~~")} func (m Monster) Say() {fmt.Println("Monster Say()~~")} func main() {//Monster实现了AInterface 和 BInterfacevar monster Monstervar a2 AInterface = monstervar b2 BInterface = monstera2.Say()b2.Hello()}

Golang 接口中不能有任何变量
一个接口(比如 A 接口)可以继承多个别的接口(比如 B,C 接口),这时如果要实现 A 接口,也必须将 B,C 接口的方法也全部实现。package mainimport ("fmt") type BInterface interface {test01()} type CInterface interface {test02()} type AInterface interface {BInterfaceCInterfacetest03()} //如果需要实现AInterface,就需要将BInterface CInterface的方法都实现type Stu struct {}func (stu Stu) test01() { }func (stu Stu) test02() {}func (stu Stu) test03() {} func main() {var stu Stuvar a AInterface = stua.test01()}interface 类型默认是一个指针(引用类型),如果没有对 interface 初始化就使用,那么会输出 nil空接口 interface{} 没有任何方法,所以所有类型都实现了空接口, 即我们可以把任何一个变量赋给空接口。type T  interface{ } func main() {var t T = stu //okfmt.Println(t)var t2 interface{}  = stuvar num1 float64 = 8.8t2 = num1t = num1fmt.Println(t2, t)}




 

原文地址:https://www.cnblogs.com/liang545621/p/13471229.html