回答編集履歴
2
コード追加
test
CHANGED
@@ -17,3 +17,193 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
なのでこのコードだと実感しづらいです。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```go
|
28
|
+
|
29
|
+
package main
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
import "fmt"
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
type Add struct {}
|
42
|
+
|
43
|
+
func (x Add) calculate(a int, b int) int { return a+b }
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
type Sub struct {}
|
48
|
+
|
49
|
+
func (x Sub) calculate(a int, b int) int { return a-b }
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
type Mul struct {}
|
54
|
+
|
55
|
+
func (x Mul) calculate(a int, b int) int { return a*b }
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
type Div struct {}
|
60
|
+
|
61
|
+
func (x Div) calculate(a int, b int) int { return a/b }
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
// 型によって作り分ける
|
68
|
+
|
69
|
+
func somethingNiceFunctionAdd(c Add, left int, right int) {
|
70
|
+
|
71
|
+
fmt.Println(c.calculate(left, right))
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
func somethingNiceFunctionSub(c Sub, left int, right int) {
|
76
|
+
|
77
|
+
fmt.Println(c.calculate(left, right))
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
func somethingNiceFunctionMul(c Mul, left int, right int) {
|
82
|
+
|
83
|
+
fmt.Println(c.calculate(left, right))
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
func somethingNiceFunctionDiv(c Div, left int, right int) {
|
88
|
+
|
89
|
+
fmt.Println(c.calculate(left, right))
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
func main() {
|
98
|
+
|
99
|
+
var a Add
|
100
|
+
|
101
|
+
var s Sub
|
102
|
+
|
103
|
+
var m Mul
|
104
|
+
|
105
|
+
var d Div
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
// 呼ぶ側も型によって使い分ける
|
110
|
+
|
111
|
+
somethingNiceFunctionAdd(a, 7, 2)
|
112
|
+
|
113
|
+
somethingNiceFunctionSub(s, 7, 2)
|
114
|
+
|
115
|
+
somethingNiceFunctionMul(m, 7, 2)
|
116
|
+
|
117
|
+
somethingNiceFunctionDiv(d, 7, 2)
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
```go
|
132
|
+
|
133
|
+
package main
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
import "fmt"
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
type Calc interface{
|
142
|
+
|
143
|
+
calculate(a int, b int) int
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
type Add struct {}
|
150
|
+
|
151
|
+
func (x Add) calculate(a int, b int) int { return a+b }
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
type Sub struct {}
|
156
|
+
|
157
|
+
func (x Sub) calculate(a int, b int) int { return a-b }
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
type Mul struct {}
|
162
|
+
|
163
|
+
func (x Mul) calculate(a int, b int) int { return a*b }
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
type Div struct {}
|
168
|
+
|
169
|
+
func (x Div) calculate(a int, b int) int { return a/b }
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
// ひとつでいい
|
174
|
+
|
175
|
+
func somethingNiceFunction(c Calc, left int, right int) {
|
176
|
+
|
177
|
+
fmt.Println(c.calculate(left, right))
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
func main() {
|
184
|
+
|
185
|
+
var a Add
|
186
|
+
|
187
|
+
var s Sub
|
188
|
+
|
189
|
+
var m Mul
|
190
|
+
|
191
|
+
var d Div
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
// 呼ぶ側も区別しなくていい
|
196
|
+
|
197
|
+
somethingNiceFunction(a, 7, 2)
|
198
|
+
|
199
|
+
somethingNiceFunction(s, 7, 2)
|
200
|
+
|
201
|
+
somethingNiceFunction(m, 7, 2)
|
202
|
+
|
203
|
+
somethingNiceFunction(d, 7, 2)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
```
|
1
修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
`Add, Sub, Mul, Div`を使う関数を書くとき(例えば計算結果を表示するだけ)
|
1
|
+
`Add, Sub, Mul, Div`のどれを使うかだけが違う関数を書くとき(例えば計算結果を表示するだけ)
|
2
2
|
|
3
3
|
インターフェースがないと引数の型が違うだけの関数を4つ書きますが、
|
4
4
|
|