golang学习笔记 ---interface

1. 什么是interface接口

interface 是GO语言的基础特性之一。可以理解为一种类型的规范或者约定。它跟java,C# 不太一样,不需要显示说明实现了某个接口,它没有继承或子类或“implements”关键字,只是通过约定的形式,隐式的实现interface 中的方法即可。因此,Golang 中的 interface 让编码更灵活、易扩展。

  如何理解go 语言中的interface ? 只需记住以下三点即可:

    1. interface 是方法声明的集合
    2. 任何类型的对象实现了在interface 接口中声明的全部方法,则表明该类型实现了该接口。
    3. interface 可以作为一种数据类型,实现了该接口的任何对象都可以给对应的接口类型变量赋值。

  注意:
    a. interface 可以被任意对象实现,一个类型/对象也可以实现多个 interface
    b. 方法不能重载,如 eat() eat(s string) 不能同时存在

2. 接口实现

package main

import "fmt"

type Phone interface {
	Call()
}

type NokiaPhone struct {
}

func (nokiaPhone NokiaPhone) Call() {
	fmt.Println("I am Nokia, I can call you!")
}

type ApplePhone struct {
}

func (iPhone ApplePhone) Call() {
	fmt.Println("I am Apple Phone, I can call you!")
}

func main() {
	var phone Phone
	phone = new(NokiaPhone)
	phone.Call()

	phone = new(ApplePhone)
	phone.Call()
}

输出结果:

I am Nokia, I can call you!

I am Apple Phone, I can call you!

 

3. interface 查询

  如果接口A实现了接口B中所有方法,那么A可以转化为B接口。 

 if varName2, ok := varName1.(interface2|typeName); ok {
    //此时 varName2 的类型由 interface1 转为 interface2,或者 varName1 不是 typeName 类型的变量
  } else {
    //不能转换 interface,或者 varName1 不是 typeName 类型的变量

  

4. interface{} 类型

  interface{} 类型没有声明任何一个方法,俗称空接口。interface{} 在我们需要存储任意类型的数值的时候相当有用。

package main

import (
	"fmt"
	"reflect"
)

func PrintAll(vals []interface{}) {
	for _, val := range vals {
		fmt.Println(val)
	}
}

func main() {
	names := []string{"stanley", "david", "oscar"}
	fmt.Println(reflect.TypeOf(names))
	vals := make([]interface{}, len(names))
	for i, v := range names {
		vals[i] = v
	}
	PrintAll(vals)
}

  然而,需要注意的是,[]T不能直接赋值给[]interface{}

 

 t := []int{1, 2, 3, 4}
 var s []interface{} = t

  

编译时会输出下面的错误:

cannot use t (type []int) as type []interface {} in assignment

 

posted on 2019-10-09 10:22  清明-心若淡定  阅读(447)  评论(0编辑  收藏  举报