回答編集履歴
1
意味的に同じ例を追加
test
CHANGED
@@ -161,3 +161,71 @@
|
|
161
161
|
参考
|
162
162
|
|
163
163
|
edx: Software Construction in Java
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
###追記(意味的に同じにしたければ)
|
168
|
+
|
169
|
+
---
|
170
|
+
|
171
|
+
numとnameがともに等しければ、おなじSampleオブジェクトだと判定する、という回答が抜けていました。
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
```Java
|
176
|
+
|
177
|
+
package test;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public class ImmutableSample {
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
private final int num;
|
186
|
+
|
187
|
+
private final String name;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
public ImmutableSample(int num, String name) {
|
192
|
+
|
193
|
+
this.num = num;
|
194
|
+
|
195
|
+
this.name = name;
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
@Override
|
202
|
+
|
203
|
+
public boolean equals(Object obj){
|
204
|
+
|
205
|
+
if(!(obj instanceof ImmutableSample)){
|
206
|
+
|
207
|
+
return false;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
ImmutableSample s = (ImmutableSample) obj;
|
212
|
+
|
213
|
+
return (s.num == this.num && s.name.equals(this.name));
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
@Override
|
220
|
+
|
221
|
+
public int hashCode(){
|
222
|
+
|
223
|
+
return (this.num + this.name.hashCode());
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
```
|