Go语言学习之interface(The way to go)
2017-04-11 14:28
831 查看
生命不止,继续Go go go.
今天跟大家分享一些golang中的interface。
golang中没有类的概念,但是有结构struct,同样也有接口interface的概念。golang中的接口与java中的接口,与C++中的纯虚函数有几分相似,但是也有很多的不同之处。
所以,还是要认真学习golang中的interface。
定义
关键字:interface
其实,通俗的讲,golang中的接口就是一系列的未实现的方法组成。
**interface可以被任意的对象实现
一个对象可以实现任意多个interface**
interface值
输出:
This is Mike, a Student:
Hi, I am Mike you can call me on 222-222-XXX
La la la la… November rain
This is Tom, an Employee:
Hi, I am Sam, I work at Things Ltd.. Call me on 444-222-XXX
La la la la… Born to be wild
Let’s use a slice of Men and see what happens
Hi, I am Paul you can call me on 111-222-XXX
Hi, I am Sam, I work at Golang Inc.. Call me on 444-222-XXX
Hi, I am Mike you can call me on 222-222-XXX
这里需要注意:
一定要实现接口中所有的方法
如果取消Men接口中的注释,则会报错:
Student does not implement Men (missing Fuck method)
空interface
空interface没有方法
接口嵌套
最后,贴上官方例子:
今天跟大家分享一些golang中的interface。
golang中没有类的概念,但是有结构struct,同样也有接口interface的概念。golang中的接口与java中的接口,与C++中的纯虚函数有几分相似,但是也有很多的不同之处。
所以,还是要认真学习golang中的interface。
定义
关键字:interface
type Men interface { SayHi() Sing(lyrics string) }
其实,通俗的讲,golang中的接口就是一系列的未实现的方法组成。
**interface可以被任意的对象实现
一个对象可以实现任意多个interface**
type Human struct { name string age int phone string } type Student struct { Human //an anonymous field of type Human school string loan float32 } type Employee struct { Human //an anonymous field of type Human company string money float32 } // A human likes to stay... err... *say* hi func (h *Human) SayHi() { fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) } // A human can sing a song, preferrably to a familiar tune! func (h *Human) Sing(lyrics string) { fmt.Println("La la, la la la, la la la la la...", lyrics) } // A Human man likes to guzzle his beer! func (h *Human) Guzzle(beerStein string) { fmt.Println("Guzzle Guzzle Guzzle...", beerStein) } // Employee's method for saying hi overrides a normal Human's one func (e *Employee) SayHi() { fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone) //Yes you can split into 2 lines here. } // A Student borrows some money func (s *Student) BorrowMoney(amount float32) { loan += amount // (again and again and...) } // An Employee spends some of his salary func (e *Employee) SpendSalary(amount float32) { e.money -= amount // More vodka please!!! Get me through the day! } // INTERFACES type Men interface { SayHi() Sing(lyrics string) Guzzle(beerStein string) } type YoungChap interface { SayHi() Sing(song string) BorrowMoney(amount float32) } type ElderlyGent interface { SayHi() Sing(song string) SpendSalary(amount float32) }
interface值
package main import "fmt" type Human struct { name string age int phone string } type Student struct { Human //an anonymous field of type Human school string loan float32 } type Employee struct { Human //an anonymous field of type Human company string money float32 } //A human method to say hi func (h Human) SayHi() { fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) } //A human can sing a song func (h Human) Sing(lyrics string) { fmt.Println("La la la la...", lyrics) } //Employee's method overrides Human's one func (e Employee) SayHi() { fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone) //Yes you can split into 2 lines here. } // Interface Men is implemented by Human, Student and Employee // because it contains methods implemented by them. type Men interface { SayHi() Sing(lyrics string) //Fuck() } func main() { mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00} paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100} sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000} Tom := Employee{Human{"Sam", 36, "444-222-XXX"}, "Things Ltd.", 5000} //a variable of the interface type Men var i Men //i can store a Student i = mike fmt.Println("This is Mike, a Student:") i.SayHi() i.Sing("November rain") //i can store an Employee too i = Tom fmt.Println("This is Tom, an Employee:") i.SayHi() i.Sing("Born to be wild") //a slice of Men fmt.Println("Let's use a slice of Men and see what happens") x := make([]Men, 3) //These elements are of different types that satisfy the Men interface x[0], x[1], x[2] = paul, sam, mike for _, value := range x { value.SayHi() } }
输出:
This is Mike, a Student:
Hi, I am Mike you can call me on 222-222-XXX
La la la la… November rain
This is Tom, an Employee:
Hi, I am Sam, I work at Things Ltd.. Call me on 444-222-XXX
La la la la… Born to be wild
Let’s use a slice of Men and see what happens
Hi, I am Paul you can call me on 111-222-XXX
Hi, I am Sam, I work at Golang Inc.. Call me on 444-222-XXX
Hi, I am Mike you can call me on 222-222-XXX
这里需要注意:
一定要实现接口中所有的方法
如果取消Men接口中的注释,则会报错:
Student does not implement Men (missing Fuck method)
空interface
空interface没有方法
// a is an empty interface variable var a interface{} var i int = 5 s := "Hello world" // These are legal statements a = i a = s
接口嵌套
type Interface interface { sort.Interface //嵌入字段sort.Interface Push(x interface{}) //a Push method to push elements into the heap Pop() interface{} //a Pop elements that pops elements from the heap }
最后,贴上官方例子:
// _Interfaces_ are named collections of method // signatures. package main import "fmt" import "math" // Here's a basic interface for geometric shapes. type geometry interface { area() float64 perim() float64 } // For our example we'll implement this interface on // `rect` and `circle` types. type rect struct { width, height float64 } type circle struct { radius float64 } // To implement an interface in Go, we just need to // implement all the methods in the interface. Here we // implement `geometry` on `rect`s. func (r rect) area() float64 { return r.width * r.height } func (r rect) perim() float64 { return 2*r.width + 2*r.height } // The implementation for `circle`s. func (c circle) area() float64 { return math.Pi * c.radius * c.radius } func (c circle) perim() float64 { return 2 * math.Pi * c.radius } // If a variable has an interface type, then we can call // methods that are in the named interface. Here's a // generic `measure` function taking advantage of this // to work on any `geometry`. func measure(g geometry) { fmt.Println(g) fmt.Println(g.area()) fmt.Println(g.perim()) } func main() { r := rect{width: 3, height: 4} c := circle{radius: 5} // The `circle` and `rect` struct types both // implement the `geometry` interface so we can use // instances of // these structs as arguments to `measure`. measure(r) measure(c) }
相关文章推荐
- Go语言学习之time包(获取当前时间戳等)(the way to go)
- Go语言学习之errors包(The way to go)
- Go语言学习之image、image/color、image/png、image/jpeg包(the way to go)
- Go语言学习之net包(The way to go)
- Go语言学习之archive/zip、compress/zlib、compress/gzip包(the way to go)
- Go语言学习之method(The way to go)
- Go语言学习之Arrays和Slices (The way to go)
- Go语言学习之字符串(The way to go)
- Go语言学习之函数(The way to go)
- Go语言学习之struct(The way to go)
- Go语言学习之bufio包(The way to go)
- Go语言学习之常量(The way to go)
- Go语言学习之Hello World(The way to go)
- Go语言学习之运算符(The way to go)
- Go语言学习之log包(The way to go)
- Go语言学习之flag包(The way to go)
- Go语言学习之变量(The way to go)
- Go语言学习之变量(The way to go)
- Go语言学习之指针(The way to go)
- Go语言学习之运算符(The way to go)