回答編集履歴
1
意味的に同じ例を追加
answer
CHANGED
@@ -79,4 +79,38 @@
|
|
79
79
|
|
80
80
|
|
81
81
|
参考
|
82
|
-
edx: Software Construction in Java
|
82
|
+
edx: Software Construction in Java
|
83
|
+
|
84
|
+
###追記(意味的に同じにしたければ)
|
85
|
+
---
|
86
|
+
numとnameがともに等しければ、おなじSampleオブジェクトだと判定する、という回答が抜けていました。
|
87
|
+
|
88
|
+
```Java
|
89
|
+
package test;
|
90
|
+
|
91
|
+
public class ImmutableSample {
|
92
|
+
|
93
|
+
private final int num;
|
94
|
+
private final String name;
|
95
|
+
|
96
|
+
public ImmutableSample(int num, String name) {
|
97
|
+
this.num = num;
|
98
|
+
this.name = name;
|
99
|
+
}
|
100
|
+
|
101
|
+
@Override
|
102
|
+
public boolean equals(Object obj){
|
103
|
+
if(!(obj instanceof ImmutableSample)){
|
104
|
+
return false;
|
105
|
+
}
|
106
|
+
ImmutableSample s = (ImmutableSample) obj;
|
107
|
+
return (s.num == this.num && s.name.equals(this.name));
|
108
|
+
}
|
109
|
+
|
110
|
+
@Override
|
111
|
+
public int hashCode(){
|
112
|
+
return (this.num + this.name.hashCode());
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
```
|