发布于 2016-01-29 05:25:49 | 147 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的GO语言教程,程序狗速度看过来!

Go语言

Go是一种新的语言,一种并发的、带垃圾回收的、快速编译的语言。Go是谷歌2009年发布的第二款编程语言。2009年7月份,谷歌曾发布了Simple语言,它是用来开发Android应用的一种BASIC语言。


这篇文章主要介绍了go语言中的interface使用实例,go语言中的interface是一组未实现的方法的集合,如果某个对象实现了接口中的所有方法,那么此对象就实现了此接口,需要的朋友可以参考下

go语言中的interface是一组未实现的方法的集合,如果某个对象实现了接口中的所有方法,那么此对象就实现了此接口。与其它面向对象语言不同的是,go中无需显示声明调用了哪个接口。


package main
 
import (
 "fmt"
)
 
type I interface {
 Get() int
 Put(int)
}
 
type S struct{ i int }
 
func (p *S) Get() int  { return p.i }
func (p *S) Put(v int) { p.i = v }
 
type R struct{ i int }
 
func (p *R) Get() int  { return p.i }
func (p *R) Put(v int) { p.i = v }
 
func f1(p I) {
 fmt.Println(p.Get())
 p.Put(1)
}
 
//interface{}空接口,能接受任何类型。.(I)是类型断言,用于转换something到I类型的接口
func f2(p interface{}) {
 if t, ok := p.(S); ok {
  fmt.Println("S:", t)
 } else if t, ok := p.(I); ok {
  fmt.Println("I:", t.Get())
 }
}
 
func f3(p interface{}) {
 switch t := p.(type) {
 case S:
  fmt.Println("S:", t.Get())
 case R:
  fmt.Println("R:", t.Get())
 case I:
  fmt.Println("I:", t.Get())
 default:
  fmt.Println("unknow type")
 }
}
 
func main() {
 s := S{101}
 
 f1(&s)
 f2(&s)
 
 r := R{1111}
 f3(&r)
}

如上结构S实现了I的两个方法,因此S实现了I。因为S实现了I,因此可以调用f向其传递S类型值得指针。
 
总结如下:
(1)使用“comma, ok” 来判断一个接口类型是否实现了某个特定接口:


if t, ok := something.(I) ; ok {
// 对于某些实现了接口I 的
// t 是其所拥有的类型
}

(2)声明为 interface 类型的变量,可以存储任何实现了 interface 中所有方法的类型的变量
(3)空接口可代表任何类型,可做形参和返回类型


package main
 
import "fmt"
 
func main() {
 //interface{}
 var i interface{} = 100
 var s interface{} = "hello"
 fmt.Printf("i = %d, s = %s\n", i, s)
 s = i
 fmt.Printf("i = %d, s = %d\n", i, s)
}

(4)interface组合

将一个 interface1 嵌入到另一个 interface2 的声明中,其作用相当于把 interface1 的函数包含到 interface2 中,但是组合中不同有重复的方法

  注:
  a. 只要两个接口中的方法列表相同(与顺序无关),即为相同的接口,可以相互赋值
  b. interface1 的方法列表属于另一个 interface2 的方法列表的子集,interface2 可以赋值给 interface1,反之不成立(因为方法缺失),interface2 中的方法会覆盖 interface1 中同名的方法
  c. 可以嵌入包中的 interface



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务