You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/02.5.md
+51-47
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Object-oriented
2
2
3
-
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 function that has a receiver, which is called `method`.
3
+
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 function that has a receiver, which is called a `method`.
4
4
5
5
## method
6
6
@@ -25,7 +25,7 @@ func main() {
25
25
fmt.Println("Area of r2 is: ", area(r2))
26
26
}
27
27
28
-
```
28
+
```
29
29
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.
30
30
31
31
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.
@@ -36,18 +36,18 @@ Figure 2.8 Relationship between function and struct
36
36
37
37
Obviously that's not cool. Also, the area should really be the property of a circle or rectangle.
38
38
39
-
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.
39
+
This is where a `method`comes to play. The `method` is a function affiliated with a type. It has similar syntax as function except, after the `func` keyword has a parameter called the `receiver`, which is the main body of that method.
40
40
41
-
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.
41
+
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.
42
42
43
43
As Rob Pike said.
44
44
45
45
"A method is a function with an implicit first argument, called a receiver."
Figure 2.9 Methods are different in different structs
98
100
99
-
In the example above, the area() methods belong to both Rectangle and Circle respectively, so the receivers are Rectangle and Circle.
101
+
In the example above, the Area() methods belong to both Rectangle and Circle respectively, so the receivers are Rectangle and Circle.
100
102
101
103
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.
102
104
@@ -105,13 +107,12 @@ Can the receiver only be a struct? Of course not. Any type can be the receiver o
105
107
Use the following format to define a customized type.
106
108
```Go
107
109
type typeName typeLiteral
108
-
```
110
+
```
109
111
Examples of customized types:
110
-
```Go
111
-
type ages int
112
112
113
+
```Go
114
+
type age int
113
115
type money float32
114
-
115
116
type months map[string]int
116
117
117
118
m:= months {
@@ -120,7 +121,8 @@ m := months {
120
121
...
121
122
"December":31,
122
123
}
123
-
```
124
+
```
125
+
124
126
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.
125
127
126
128
Let's get back to talking about `method`.
@@ -139,23 +141,24 @@ const (
139
141
YELLOW
140
142
)
141
143
142
-
typeColorbyte
143
-
144
144
typeBoxstruct {
145
145
width, height, depth float64
146
-
color Color
146
+
color Color
147
147
}
148
-
148
+
typeColorbyte
149
149
typeBoxList []Box//a slice of boxes
150
150
151
+
// method
151
152
func(bBox) Volume() float64 {
152
153
return b.width * b.height * b.depth
153
154
}
154
155
156
+
// method with a pointer receiver
155
157
func(b *Box) SetColor(cColor) {
156
158
b.color = c
157
159
}
158
160
161
+
// method
159
162
func(blBoxList) BiggestsColor() Color {
160
163
v:=0.00
161
164
k:=Color(WHITE)
@@ -168,12 +171,14 @@ func (bl BoxList) BiggestsColor() Color {
fmt.Println("The color of the last one is", boxes[len(boxes)-1].color.String())
195
200
fmt.Println("The biggest one is", boxes.BiggestsColor().String())
196
201
197
-
fmt.Println("Let's paint them all black")
202
+
//Let's paint them all black
198
203
boxes.PaintItBlack()
199
-
fmt.Println("The color of the second one is", boxes[1].color.String())
200
204
205
+
fmt.Println("The color of the second one is", boxes[1].color.String())
201
206
fmt.Println("Obviously, now, the biggest one is", boxes.BiggestsColor().String())
202
207
}
208
+
```
203
209
204
-
```
205
210
We define some constants and customized types.
206
211
207
212
- Use `Color` as alias of `byte`.
@@ -210,11 +215,11 @@ We define some constants and customized types.
210
215
211
216
Then we defined some methods for our customized types.
212
217
213
-
- Volume() uses Box as its receiver and returns the volume of Box.
214
-
- SetColor(c Color) changes Box's color.
215
-
- BiggestsColor() returns the color which has the biggest volume.
216
-
- PaintItBlack() sets color for all Box in BoxList to black.
217
-
- String() use Color as its receiver, returns the string format of color name.
218
+
-`Volume()` uses Box as its receiver and returns the volume of Box.
219
+
-`SetColor(`c Color) changes Box's color.
220
+
-`BiggestsColor()` returns the color which has the biggest volume.
221
+
-`PaintItBlack()` sets color for all Box in BoxList to black.
222
+
-`String()` use Color as its receiver, returns the string format of color name.
218
223
219
224
Is it much clearer when we use words to describe our requirements? We often write our requirements before we start coding.
220
225
@@ -224,13 +229,13 @@ Let's take a look at `SetColor` method. Its receiver is a pointer of Box. Yes, y
224
229
225
230
If we see that a receiver is the first argument of a method, it's not hard to understand how it works.
226
231
227
-
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?
232
+
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?
228
233
229
234
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!
230
235
231
236
### Inheritance of method
232
237
233
-
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.
238
+
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.
0 commit comments