提交 8e3a6026 编写于 作者: A Anchor

Grammar and typo fixes for 02.5.md

上级 61a6b424
# Object-oriented
We talked about functions and structs in the last two sections, did you ever think about using functions as fields of a struct? In this section, I will introduce you another form of method that has receiver, which is called `method`.
We talked about functions and structs in the last two sections, but did you ever consider using functions as fields of a struct? In this section, I will introduce you to another form of method that has a receiver, which is called `method`.
## method
Suppose you define a struct of rectangle, and you want to calculate its area, we usually use following code to achieve this goal.
Suppose you define a "rectangle" struct and you want to calculate its area. We'd typically use the following code to achieve this goal.
package main
import "fmt"
......@@ -24,19 +24,19 @@ Suppose you define a struct of rectangle, and you want to calculate its area, we
fmt.Println("Area of r2 is: ", area(r2))
}
Above example can calculate rectangle's area, we use the function called `area`, but it's not a method of a rectangle struct (like methods in class in classic Object-oriented language). The function and struct are two independent things as you may notice.
The above example can calculate a rectangle's area. We use the function called `area`, but it's not a method of the rectangle struct (like class methods in classic object-oriented languages). The function and struct are two independent things as you may notice.
It's not a problem so far. What if you also have to calculate area of circle, square, pentagon, even more, you are going to add more functions with very similar name.
It's not a problem so far. However, if you also have to calculate the area of a circle, square, pentagon, or any other kind of shape, you are going to need to add additional functions with very similar names.
![](images/2.5.rect_func_without_receiver.png?raw=true)
Figure 2.8 Relationship between function and struct
Obviously, it's not cool. Also the area should be the property of circle or rectangle.
Obviously that's not cool. Also, the area should really be the property of a circle or rectangle.
For those reasons, we have concepts about `method`. `method` is affiliated of type, it has same syntax as function except one more thing after the keyword `func` that is called `receiver` which is the main body of that method.
For those reasons, we have the `method` concept. `method` is affiliated with type. It has the same syntax as functions do except for an additional parameter after the `func` keyword called the `receiver`, which is the main body of that method.
Use the same example, `Rectangle.area()` belongs to rectangle, not as a peripheral function. More specifically, `length`, `width` and `area()` all belong to rectangle.
Using the same example, `Rectangle.area()` belongs directly to rectangle, instead of as a peripheral function. More specifically, `length`, `width` and `area()` all belong to rectangle.
As Rob Pike said.
......@@ -46,7 +46,7 @@ Syntax of method.
func (r ReceiverType) funcName(parameters) (results)
Let's change out example by using method.
Let's change our example using `method` instead.
package main
import (
......@@ -84,25 +84,25 @@ Let's change out example by using method.
Notes for using methods.
- If the name of methods is same, but they don't have same receivers, they are not same.
- methods are able to access fields in receivers.
- Use `.` to call a method in the struct, just like to call fields.
- If the name of methods are the same but they don't share the same receivers, they are not the same.
- Methods are able to access fields within receivers.
- Use `.` to call a method in the struct, the same way fields are called.
![](images/2.5.shapes_func_with_receiver_cp.png?raw=true)
Figure 2.9 Methods are difference in difference struct
Figure 2.9 Methods are different in different structs
In above example, method area() is respectively belonging to Rectangle and Circle, so the receivers are Rectangle and Circle.
In the example above, the area() methods belong to both Rectangle and Circle respectively, so the receivers are Rectangle and Circle.
One thing is worthy of note that the method with a dotted line means the receiver is passed by value, not by reference. The different between them is that the method could change receiver's value when the receiver is passed by reference, and it gets the copy of receiver when the receiver is passed by value.
One thing that's worth noting is that the method with a dotted line means the receiver is passed by value, not by reference. The difference between them is that a method can change its receiver's values when the receiver is passed by reference, and it gets a copy of the receiver when the receiver is passed by value.
Does the receiver can only be struct? Of course not, any type could be the receiver of a method. You may be confused about customized type, struct is a special type of customized type, there are more customized types.
Can the receiver only be a struct? Of course not. Any type can be the receiver of a method. You may be confused about customized types. Struct is a special kind of customized type -there are more customized types.
Use following format to define a customized type.
Use the following format to define a customized type.
type typeName typeLiteral
Examples about customized type.
Examples of customized types:
type ages int
......@@ -117,11 +117,11 @@ Examples about customized type.
"December":31,
}
I hope you know how to use customized type now. it's like `typedef` in C, we use `ages` to substitute `int` in above example.
I hope that you know how to use customized types now. Similar to `typedef` in C, we use `ages` to substitute `int` in the above example.
Let's get back to `method`.
Let's get back to talking about `method`.
You can use as many methods in customized types as you want.
You can use as many methods in custom types as you want.
package main
import "fmt"
......@@ -204,27 +204,27 @@ We define some constants and customized types.
Then we defined some methods for our customized types.
- Volume() use Box as its receiver, returns volume of Box.
- Volume() uses Box as its receiver and returns volume of Box.
- SetColor(c Color) changes Box's color.
- BiggestsColor() returns the color which has the biggest volume.
- PaintItBlack() sets color for all Box in BoxList to black.
- String() use Color as its receiver, returns the string format of color name.
Is it much clear when we use words to describe our requirements? We often write our requirements before we start coding.
Is it much clearer when we use words to describe our requirements? We often write our requirements before we start coding.
### Use pointer as receiver
Let's take a look at method `SetColor`, its receiver is a pointer of Box. Yes, you can use `*Box` as receiver. Why we use pointer here? Because we want to change Box's color in this method, if we don't use pointer, it only changes value of copy of Box.
Let's take a look at `SetColor` method. Its receiver is a pointer of Box. Yes, you can use `*Box` as a receiver. Why do we use a pointer here? Because we want to change Box's color in this method. Thus, if we don't use a pointer, it will only change the value inside a copy of Box.
If we see receiver as the first argument of the method, it's not hard to understand how it works.
If we see that a receiver is the first argument of a method, it's not hard to understand how it works.
You may ask that we should use `(*b).Color=c` instead of `b.Color=c` in method SetColor(). Either one is OK here because Go knows it. Do you think Go is more fascinating now?
You might be asking why we aren't using `(*b).Color=c` instead of `b.Color=c` in the SetColor() method. Either one is OK here because Go knows how to interpret the assignment. Do you think Go is more fascinating now?
You may also ask we should use `(&bl[i]).SetColor(BLACK)` in `PaintItBlack` because we pass a pointer to `SetColor`. One more time, either one is OK because Go knows it!
You may also be asking whether we should use `(&bl[i]).SetColor(BLACK)` in `PaintItBlack` because we pass a pointer to `SetColor`. Again, either one is OK because Go knows how to interpret it!
### Inheritance of method
We learned inheritance of field in last section, and we also have inheritance of method in Go. So that if a anonymous field has methods, then the struct that contains the field have all methods from it as well.
We learned about inheritance of fields in the last section. Similarly, we also have method inheritance in Go. If an anonymous field has methods, then the struct that contains the field will have all the methods from it as well.
package main
import "fmt"
......@@ -260,7 +260,7 @@ We learned inheritance of field in last section, and we also have inheritance of
### Method overload
If we want Employee to have its own method `SayHi`, we can define the method that has same name in Employee, and it will hide `SayHi` in Human when we call it.
If we want Employee to have its own method `SayHi`, we can define a method that has the same name in Employee, and it will hide `SayHi` in Human when we call it.
package main
import "fmt"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册