修改了一下语句

This commit is contained in:
astaxie
2012-09-03 09:36:41 +08:00
parent 0357d8b3ff
commit e2f10b4038

16
2.6.md
View File

@@ -1,19 +1,19 @@
#2.6interface #2.6interface
##interface ##interface
Go语言里面设计最精妙的应该算interface它让面向对象内容组织实现非常的方便当你看完这一章你就会被interface的设计巧妙所折服。 Go语言里面设计最精妙的应该算interface它让面向对象内容组织实现非常的方便当你看完这一章你就会被interface的巧妙设计所折服。
###什么是interface ###什么是interface
简单的说interface是一组method的组合我们通过interface来定义一个对象的一组行为。 简单的说interface是一组method的组合我们通过interface来定义对象的一组行为。
我们前面一章最后一个例子中Student和Employee都能Sayhi虽然他们的内部实现不一样但是那不重要重要的是他们都能`say hi` 我们前面一章最后一个例子中Student和Employee都能Sayhi虽然他们的内部实现不一样但是那不重要重要的是他们都能`say hi`
让我们来继续做更多的扩展Student和Employee实现另一个方法`Sing`然后Student实现方法BorrowMoney而Employee实现SpendSalary。 让我们来继续做更多的扩展Student和Employee实现另一个方法`Sing`然后Student实现方法BorrowMoney而Employee实现SpendSalary。
这样Student实现了三个方法Sayhi、Sing、BorrowMoney而Employee实现了Sayhi、Sing、SpendSalary。 这样Student实现了三个方法Sayhi、Sing、BorrowMoney而Employee实现了Sayhi、Sing、SpendSalary。
这些Student和Employee实现的一组方法就是interface。例如Student和Employee都满足了这个interfaceSayhi和Sing。而Employee没有满足这个interfaceSayhi、Sing和BorrowMoney因为Employee没有实现BorrowMoney这个方法。 上面这些方法的组合称为interface(被对象Student和Employee实现)。例如Student和Employee都实现了interfaceSayhi和Sing也就是这两个对象是该interface类型。而Employee没有实现这个interfaceSayhi、Sing和BorrowMoney因为Employee没有实现BorrowMoney这个方法。
###interface类型 ###interface类型
interface类型定义一组方法,这组方法被一些对象实现。详细的语法参考下面这个例子 interface类型定义一组方法,如果某个对象实现了某个接口的所有方法,则此对象实现了此接口。详细的语法参考下面这个例子
type Human struct { type Human struct {
name string name string
@@ -43,7 +43,7 @@ interface类型是定义一组方法这组方法被一些对象实现。详
fmt.Println("La la, la la la, la la la la la...", lyrics) fmt.Println("La la, la la la, la la la la la...", lyrics)
} }
//Human对象实现Guzzle放噶 //Human对象实现Guzzle方法
func (h *Human) Guzzle(beerStein string) { func (h *Human) Guzzle(beerStein string) {
fmt.Println("Guzzle Guzzle Guzzle...", beerStein) fmt.Println("Guzzle Guzzle Guzzle...", beerStein)
} }
@@ -83,7 +83,7 @@ interface类型是定义一组方法这组方法被一些对象实现。详
SpendSalary(amount float32) SpendSalary(amount float32)
} }
通过上面的代码我们可以知道interface可以被任意的对象实现。我们看到上面的Men interface被Human、Student和Employee实现。同理一个对象可以实现任意多个interface例如上面的Student实现了Men和YonggChap两个interface。 通过上面的代码我们可以知道interface可以被任意的对象实现。我们看到上面的Men interface被Human、Student和Employee实现。同理一个对象可以实现任意多个interface例如上面的Student实现了Men和YonggChap两个interface。
最后任意的类型都实现了空interface(我们这样定义interface{})也就是包含0个method的interface。 最后任意的类型都实现了空interface(我们这样定义interface{})也就是包含0个method的interface。
@@ -345,7 +345,7 @@ Go里面真正吸引人的是他内置的逻辑语法就像我们在学习Str
Swap(i, j int) Swap(i, j int)
} }
另一个例子就是io包下面的 io.ReadWriter 他包含了io包下面的Reader和Writer两个interface。 另一个例子就是io包下面的 io.ReadWriter 他包含了io包下面的Reader和Writer两个interface。
// io.ReadWriter // io.ReadWriter
type ReadWriter interface { type ReadWriter interface {