golang接口

1、接口快速入门

package main

import (
	"fmt"
)

//声明/定义一个接口
type Usb interface {
	//声明两个没有实现的方法
	Start()
	Stop()
}

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变量会根据传入的参数来判断到底是phone还是camera
func (c Computer) Working(usb Usb) {
	//通过usb接口变量来调用start和stop方法
	usb.Start()
	usb.Stop()
}

func main() {
	computer := Computer{}
	phone := Phone{}
	carema := Camera{}

	computer.Working(phone)
	computer.Working(carema)
}

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

2、基本语法在这里插入图片描述

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

3、接口注意事项和细节

(1)接口本身不能创建实例,但是可以指向一一个实现了该接口的自定义类型的变量(实例)

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

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

type integer int

func (i integer) Say()  {
	fmt.Println("integer say i=",i)
}
func main() {
	var i integer = 10
	var b AInterface = i
	b.Say()
}

(6)一个自定义类型可以实现多个接口

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 a1 AInterface = monster
	var b1 BInterface = monster
	a1.Say()
	b1.Hello()
}

(7)golang接口中不能有任何变量
(8)一个接口(比如A接口)可以继承多个别的接口(比如B,C接口),这时如果要实现A接口,也必须将B,C接口的方法也全部实现。

type AInterface interface {
	Say()
}
type BInterface interface {
	Hello()
}
type CInterface interface {
	AInterface
	BInterface
	test()
}
//如果需要实现Cinterface,就需要将AInterface BInterface的方法搜实现
type Student struct {
	Name string
}

func (stu Student) Say() {
	fmt.Println("stu say()")
}
func (stu Student) Hello() {
	fmt.Println("stu hello()")
}
func (stu Student) test() {
	fmt.Println("stu test()")
}

(9) interface 类型默认是一个指针(引用类型),如果没有对interface初始化就使用,那么会输出nil
(10)空接口interface {} 没有任何方法,所以所有类型都实现了空接口,即我们可以把任何-一个变量赋给空接口。

type T interface {
	//空接口
}

func main() {
	var stu Student
	var t T = stu
	fmt.Println(t)
	var t2 interface{} = stu
	var num1 float64 = 2.2
	t2 = num1
	t = num1
	fmt.Println(t2, t)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值