diff --git a/2.6.md b/2.6.md index 69a0a7f4..be1b5fb7 100644 --- a/2.6.md +++ b/2.6.md @@ -1,19 +1,19 @@ #2.6interface ##interface -Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的设计巧妙所折服。 +Go语言里面设计最精妙的应该算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实现了三个方法:Sayhi、Sing、BorrowMoney;而Employee实现了Sayhi、Sing、SpendSalary。 -这些Student和Employee实现的一组方法就是interface。例如Student和Employee都满足了这个interface:Sayhi和Sing。而Employee就没有满足这个interface:Sayhi、Sing和BorrowMoney,因为Employee没有实现BorrowMoney这个方法。 +上面这些方法的组合称为interface(被对象Student和Employee实现)。例如Student和Employee都实现了interface:Sayhi和Sing,也就是这两个对象是该interface类型。而Employee没有实现这个interface:Sayhi、Sing和BorrowMoney,因为Employee没有实现BorrowMoney这个方法。 ###interface类型 -interface类型是定义一组方法,这组方法被一些对象实现。详细的语法参考下面这个例子 +interface类型定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。详细的语法参考下面这个例子 type Human struct { name string @@ -43,7 +43,7 @@ interface类型是定义一组方法,这组方法被一些对象实现。详 fmt.Println("La la, la la la, la la la la la...", lyrics) } - //Human对象实现Guzzle放噶 + //Human对象实现Guzzle方法 func (h *Human) Guzzle(beerStein string) { fmt.Println("Guzzle Guzzle Guzzle...", beerStein) } @@ -83,7 +83,7 @@ interface类型是定义一组方法,这组方法被一些对象实现。详 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。 @@ -345,7 +345,7 @@ Go里面真正吸引人的是他内置的逻辑语法,就像我们在学习Str Swap(i, j int) } -另一个例子就是io包下面的 io.ReadWriter ,他包含了io包下面的Reader和Writer两个interface。 +另一个例子就是io包下面的 io.ReadWriter ,他包含了io包下面的Reader和Writer两个interface。 // io.ReadWriter type ReadWriter interface {