前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang 入门系列(四)如何理解interface接口

Golang 入门系列(四)如何理解interface接口

作者头像
架构师精进
发布2018-10-18 09:55:01
4140
发布2018-10-18 09:55:01
举报
文章被收录于专栏:公众号文章公众号文章

前面讲了很多Go 语言的基础知识,包括go环境的安装,go语言的语法等,感兴趣的朋友,可以先看看之前的文章。https://www.cnblogs.com/zhangweizhong/category/1275863.html

今天就正式开始写Go 的代码,讲讲如何理解interface接口。

什么是interface接口

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

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

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

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

接口实现

代码语言:javascript
复制
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()
}

interface 查询

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

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

interface{} 类型

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

代码语言:javascript
复制
package main

import (    "fmt")

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

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

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

代码语言:javascript
复制
        t := []int{1, 2, 3, 4}        var s []interface{} = t

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

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

最后

  以上,对Go 语言中的接口特性做了一个简单的介绍。我觉得对于go语言来说,设计最精妙的应该是interface了,感兴趣的,可以好好研究研究。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-09-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 架构师精进 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档