-
Notifications
You must be signed in to change notification settings - Fork 8
/
interfaces.html
234 lines (183 loc) · 10.8 KB
/
interfaces.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 接口</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'methods';
}
if (e.key == "ArrowRight") {
window.location.href = 'embedding';
}
}
</script>
<body>
<div class="example" id="interfaces">
<h2><a href="./">Go by Example 中文版</a>: 接口</h2>
<table>
<tr>
<td class="docs">
<p>方法签名的集合叫做:_接口(Interfaces)_。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/QXmP59vePbh"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"math"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这是一个几何体的基本接口。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">type</span> <span class="nx">geometry</span> <span class="kd">interface</span> <span class="p">{</span>
<span class="nf">area</span><span class="p">()</span> <span class="kt">float64</span>
<span class="nf">perim</span><span class="p">()</span> <span class="kt">float64</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>在这个例子中,我们将为 <code>rect</code> 和 <code>circle</code> 实现该接口。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">type</span> <span class="nx">rect</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">width</span><span class="p">,</span> <span class="nx">height</span> <span class="kt">float64</span>
<span class="p">}</span>
<span class="kd">type</span> <span class="nx">circle</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">radius</span> <span class="kt">float64</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>要在 Go 中实现一个接口,我们只需要实现接口中的所有方法。
这里我们为 <code>rect</code> 实现了 <code>geometry</code> 接口。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="p">(</span><span class="nx">r</span> <span class="nx">rect</span><span class="p">)</span> <span class="nf">area</span><span class="p">()</span> <span class="kt">float64</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">r</span><span class="p">.</span><span class="nx">width</span> <span class="o">*</span> <span class="nx">r</span><span class="p">.</span><span class="nx">height</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="p">(</span><span class="nx">r</span> <span class="nx">rect</span><span class="p">)</span> <span class="nf">perim</span><span class="p">()</span> <span class="kt">float64</span> <span class="p">{</span>
<span class="k">return</span> <span class="mi">2</span><span class="o">*</span><span class="nx">r</span><span class="p">.</span><span class="nx">width</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="nx">r</span><span class="p">.</span><span class="nx">height</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>circle</code> 的实现。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="p">(</span><span class="nx">c</span> <span class="nx">circle</span><span class="p">)</span> <span class="nf">area</span><span class="p">()</span> <span class="kt">float64</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">math</span><span class="p">.</span><span class="nx">Pi</span> <span class="o">*</span> <span class="nx">c</span><span class="p">.</span><span class="nx">radius</span> <span class="o">*</span> <span class="nx">c</span><span class="p">.</span><span class="nx">radius</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="p">(</span><span class="nx">c</span> <span class="nx">circle</span><span class="p">)</span> <span class="nf">perim</span><span class="p">()</span> <span class="kt">float64</span> <span class="p">{</span>
<span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="nx">math</span><span class="p">.</span><span class="nx">Pi</span> <span class="o">*</span> <span class="nx">c</span><span class="p">.</span><span class="nx">radius</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>如果一个变量实现了某个接口,我们就可以调用指定接口中的方法。
这儿有一个通用的 <code>measure</code> 函数,我们可以通过它来使用所有的 <code>geometry</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">measure</span><span class="p">(</span><span class="nx">g</span> <span class="nx">geometry</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">g</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">g</span><span class="p">.</span><span class="nf">area</span><span class="p">())</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">g</span><span class="p">.</span><span class="nf">perim</span><span class="p">())</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">r</span> <span class="o">:=</span> <span class="nx">rect</span><span class="p">{</span><span class="nx">width</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="nx">height</span><span class="p">:</span> <span class="mi">4</span><span class="p">}</span>
<span class="nx">c</span> <span class="o">:=</span> <span class="nx">circle</span><span class="p">{</span><span class="nx">radius</span><span class="p">:</span> <span class="mi">5</span><span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>结构体类型 <code>circle</code> 和 <code>rect</code> 都实现了 <code>geometry</code> 接口,
所以我们可以将其实例作为 <code>measure</code> 的参数。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nf">measure</span><span class="p">(</span><span class="nx">r</span><span class="p">)</span>
<span class="nf">measure</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> go run interfaces.go
<span class="go">{3 4}
</span><span class="go">12
</span><span class="go">14
</span><span class="go">{5}
</span><span class="go">78.53981633974483
</span><span class="go">31.41592653589793</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>要学习更多关于 Go 接口的知识,
可以看看这篇<a href="http://jordanorelli.tumblr.com/post/32665860244/how-to-use-interfaces-in-go">很棒的博文</a>。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="embedding">Embedding</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"math\"\u000A)\u000A');codeLines.push('type geometry interface {\u000A area() float64\u000A perim() float64\u000A}\u000A');codeLines.push('type rect struct {\u000A width, height float64\u000A}\u000Atype circle struct {\u000A radius float64\u000A}\u000A');codeLines.push('func (r rect) area() float64 {\u000A return r.width * r.height\u000A}\u000Afunc (r rect) perim() float64 {\u000A return 2*r.width + 2*r.height\u000A}\u000A');codeLines.push('func (c circle) area() float64 {\u000A return math.Pi * c.radius * c.radius\u000A}\u000Afunc (c circle) perim() float64 {\u000A return 2 * math.Pi * c.radius\u000A}\u000A');codeLines.push('func measure(g geometry) {\u000A fmt.Println(g)\u000A fmt.Println(g.area())\u000A fmt.Println(g.perim())\u000A}\u000A');codeLines.push('func main() {\u000A r :\u003D rect{width: 3, height: 4}\u000A c :\u003D circle{radius: 5}\u000A');codeLines.push(' measure(r)\u000A measure(c)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>