Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5430411
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: LINUX

2013-08-01 17:09:20


点击(此处)折叠或打开

  1. 一、什么是interface
  2.         简单地说,interface是一组method的组合,可以通过interface来定义对象的一组行为。
  3.         
  4. 二、interface类型
  5.         interface类型定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。详细语法参考如下例子:
  6.         type Human struct {
  7.             name string
  8.             age int
  9.             phone string
  10.         }
  11.         type Student struct {
  12.             Human //匿名字段Human
  13.             school string
  14.             loan float32
  15.         }
  16.         type Employee struct {
  17.             Human //匿名字段Human
  18.             company string
  19.             money float32
  20.         }
  21.         //Human对象实现Sayhi方法
  22.         func (h *Human) SayHi() {
  23.             fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
  24.         }
  25.         //Human对象实现Sing方法
  26.         func (h *Human) Sing(lyrics string) {
  27.             fmt.Println("La la, la la la, la la la la...", lyrics)
  28.         }
  29.         //Human对象实现Guzzle方法
  30.         func (h *Human) Guzzle(beerStein string) {
  31.             fmt.Println("Guzzle Guzzle Guzzle...", beerStein)
  32.         }
  33.         //Employee重载Human的Sayhi方法
  34.         func (e *Employee) SayHi() {
  35.             fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone)
  36.         }
  37.         //Student实现BorrowMoney方法
  38.         func (s *Student) BorrowMoney(amount float32) {
  39.             s.loan += amount
  40.         }
  41.         //Employee实现SpendSalary方法
  42.         func (e *Employee) SpendSalary(amount float32) {
  43.             e.money -= amount
  44.         }
  45.         //定义interface
  46.         type Men interface {
  47.             SayHi()
  48.             Sing(lyrics string)
  49.             Guzzle(beerStein string)
  50.         }
  51.         type YoungChap interface {
  52.             SayHi()
  53.             Sing(song string)
  54.             BorrowMoney(amount float32)
  55.         }
  56.         type ElderlyGent interface {
  57.             SayHi()
  58.             Sing(song string)
  59.             SpendSalary(amount float32)
  60.         }

  61.         从上面代码可知,interface可以被任意的对象实现,比如:Men interface被Human、Student和Employee实现。同理,一个对象可以实现任意多个interface,比如:Student实现了Men和YonggChap两个interface。
  62.         任意类型都实现了空interface(我们这样定义:interface{}),即包含0个method的interface。

  63. 三、interface值
  64.         如果我们定义了一个interface的变量,那么这个变量可以存储实现了这个interface的任意类型的对象。比如:上例中定了一个Men interface类型的变量m,那么m里面可以存储Human、Student或者Employee值。下面举例说明如何使用:因为m能够持有这三种类型的对象,所以可以定义一个包含Men类型元素的slice,这个slice可以被赋予实现了Men接口的任意结构的对象:
  65.        
  66.         package main
  67.         import "fmt"
  68.         type Human struct {
  69.             name string
  70.             age int
  71.             phone string
  72.         }
  73.         type Student struct {
  74.             Human //匿名字段
  75.             school string
  76.             loan float32
  77.         }
  78.         type Employee struct {
  79.             Human //匿名字段
  80.             company string
  81.             money float32
  82.         }
  83.         //Human实现Sayhi方法
  84.         func (h Human) SayHi() {
  85.             fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
  86.         }
  87.         //Human实现Sing方法
  88.         func (h Human) Sing(lyrics string) {
  89.             fmt.Println("La la la la...", lyrics)
  90.         }
  91.         //Employee重载Human的SayHi方法
  92.         func (e Employee) SayHi() {
  93.             fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name.e.company, e.phone)
  94.         }
  95.         //Interface Men被Human、Student和Employee实现
  96.         //因为这三个类型都实现了这了两个方法
  97.         type Men interface {
  98.             SayHi()
  99.             Sing(lyrics string)
  100.         }
  101.         func main() {
  102.             mike := Student{Human{"Mike", 25, "222-222-xxx"}, "MIT", 0.00}
  103.             paul := Student{Human{"Paul", 26, "111-222-xxx"}, "Harvard", 100}
  104.             sam := Employee{Human{"Sam", 36, "444-222-xxx"}, "Golang Inc.", 1000}
  105.             Tom := Employee{Human{"Sam", 36, "444-222-xxx"}, "Things Ltd.", 5000}
  106.             //定义Men类型的变量i
  107.             var i Men
  108.             //i能存储Student
  109.             i = mike
  110.             fmt.Println("This is Mike, a Student:")
  111.             i.SayHi()
  112.             i.Sing("November rain")
  113.             //i也能存储Employee
  114.             i = Tom
  115.             fmt.Println("This is Tom, an Employee:")
  116.             i.SayHi()
  117.             i.Sing("Born to be wild")
  118.             //定义了slice Men
  119.             fmt.Println("Let's use a slice of Men and see what happens")
  120.             x := make{[]Men, 3}
  121.             //T这三个都是不同类型的元素,但是他们实现了interface同一接口
  122.             x[0], x[1], x[2] = paul, sam, mike
  123.             for _, value := range x{
  124.                 value.SayHi()
  125.             }
  126.         }
  127.         通过上面代码,可以看出interface就是一组抽象方法的集合,必须由其它非interface类型实现,而不能自我实现,Go语言通过interface实现了duck-typing,即“当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。”

  128. 四、空interface
  129.         空interface即interface{}不包含任何的method,因此,所有类型都实现了空interface。空interface对于描述起不到任何作用,因为不包含任何的method,但是空interface在我们需要存储任意类型的数值时相当有用,因为它可以存储任意类型的数值,有点类似于C语言的void *类型。
  130.         //定义a为空接口
  131.         var a interface{}
  132.         var i int = 5
  133.         s := "hello world"
  134.         //a可以存储任意类型的数据
  135.         a = i
  136.         a = s
  137.         一个函数把interface{}作为参数,那么它可以接受任意类型的值作为参数,如果一个函数返回interface{},就可以返回任意类型的值。

  138. 五、interface函数参数
  139.         interface的变量可以持有任意实现该interface类型的对象,这样我们可以通过定义interface参数让函数接受各种类型的参数。
  140.         举例说明:fmt.Println可以接受任意类型的数据,打开fmt的源码能看到定义如下:
  141.         type Stringer interface {
  142.             String() string
  143.         }
  144.         也就是说,任何实现了String方法的类型都能作为参数被fmt.Println调用:
  145.         package main
  146.         import (
  147.             "fmt"
  148.             "strconv"
  149.         )
  150.         type Human struct {
  151.             name string
  152.             age int
  153.             phone string
  154.         }
  155.         //通过这个方法Human实现了fmt.Stringer
  156.         func (h Human) String() string {
  157.             return " "+h.name+" - "+strconv.Itoa(h.age)+" years - "+h.phone+" "
  158.         }
  159.         func main() {
  160.             Bob := Human{"Bob", 39, "000-7777-xxx"}
  161.             fmt.Println("This Human is : ", Bob)
  162.         }
  163.         如果需要某个类型能被fmt包以特殊的格式输出,就必须实现Stringer这个接口。如果没有实现这个接口,fmt将以默认的方式输出。
  164.         需要注意的是:实现了error接口的对象(即实现了Error() string的对象),使用fmt输出时,会调用Error()方法,因此不必再定义了String()方法了。

  165. 六、interface变量存储的类型
  166.         通过前文我们知道interface变量能存储任意类型的数值,只要该类型实现了interface。如果想反向知道该变量里实际存储的类型时什么该当如何?目前常用的有两种方法:
  167. 1、Comma-ok断言
  168.         在Go语言里,可以通过如下语法来判断interface里存储的数值类型:
  169.         value, ok = element.(T)
  170.         这里value就是变量的值,ok是一个bool类型,element是interface变量,T是断言的类型。如果element里面确实存储了T类型的值,则ok返回true,否则返回false。
  171.         比如:value, ok := element.(int)
  172.         这种方式的问题在于需要一长串的ifelse,断言的类型T越多则ifelse越多,因此,switch来了。
  173. 2、switch测试
  174.         语法同C语言中的switch,具体如下:
  175.         switch value := element.(type) {
  176.             case int:
  177.             ...
  178.             case string:
  179.             ...
  180.             default:
  181.             ...
  182.         }
  183.         需要注意的是:element.(type)语法不能在switch外的任何逻辑里面使用,如果要在switch外面判断一个类型就使用comma-ok。

  184. 七、嵌入interface
  185.         Go语言里面真正吸引人的是其内置的逻辑语法,就像struct中匿名字段一样,实际上,相同的逻辑在interface中也成立。如果一个interface1作为interface2的一个嵌入字段,则interface2隐式的包含了interface1里面的method。从interface源码中可以看到如下定义:
  186.         type Interface interface {
  187.             sort.Interface //嵌入字段sort.Interface
  188.             Push(x interface{}) //a Push method to push elements into the heap
  189.             Pop() interface{} //a Pop elements that pops elements from the heap
  190.         }
  191.         sort.Interface其实就是嵌入字段,把sort.Interface的所有method隐式包含进来了,也就是下面三个方法:
  192.         type Interface interface {
  193.             //Len is the number of elements in the collection.
  194.             Len() int
  195.             //Less returns whether the element with index i should sort before the element with index j.
  196.             Less(i, j int) bool
  197.             //Swap swaps the elements with indexes i and j.
  198.             Swap(i, j int)
  199.         }
  200.         另一个例子就是io包下面的io.ReadWriter,它包含了io包下面的Reader和Writer两个interface。
  201.         //io.ReadWriter
  202.         type ReadWriter interface {
  203.             Reader
  204.             Writer
  205.         }
  206.         
  207. 八、反射
  208.         Go语言实现了反射,所谓反射就是动态运行时的状态。我们一般用到的包是reflect包,官方的这篇文章详细地讲解了reflect包的实现原理——《Laws of reflection》。使用reflect一般分成三步:要去反射是一个类型的值(这些值都实现了空interface),首先需要把它转换成reflect对象(reflect.Type或者reflect.Value,根据不同情况调用不同函数)。这两种获取方式如下:
  209.         t := reflect.TypeOf(i) //得到类型的元数据,通过t我们能获取类型定义里面的所有元素
  210.         v := reflect.ValueOf(i) //得到实际的值,通过v我们获取存储在里面的值,还可以去改变值
  211.         转化为reflect对象之后就可以进行一些操作了,即将reflect对象转化成相应的值:
  212.         tag := t.Elem().Field(0).Tag //获取定义在struct里面的标签
  213.         name := v.Elem().Field(0).String() //获取存储在第一个字段里面的值
  214.         获取反射值能返回相应的类型和数值。
  215.         var x float64 = 3.4
  216.         v := reflect.ValueOf(x)
  217.         fmt.Println("type:", v.Type())
  218.         fmt.Println("kind is float64:", v.kind() == reflect.Float64)
  219.         fmt.Println("value:", v.Float())
  220.         最后,反射的字段必须是可读写的,类似于传值和传引用。按照下面的方式会产生错误:
  221.         var x float64 = 3.4
  222.         v := reflect.ValueOf(x)
  223.         v.SetFloat(7.1)
  224.         如果要修改相应的值,必须如下才行:
  225.         var x float64 = 3.4
  226.         p := reflect.ValueOf(&x)
  227.         v := p.Elem()
  228.         v.SetFloat(7.1)

阅读(2131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~