質問編集履歴

2

追記

2018/11/03 15:26

投稿

aiueoaiueoaiue
aiueoaiueoaiue

スコア94

test CHANGED
File without changes
test CHANGED
@@ -66,23 +66,7 @@
66
66
 
67
67
  Eex other = (Eex) obj;
68
68
 
69
- if (A == null) {
70
-
71
- if (other.A != null)return false;
69
+ return this.A == other.A && this.B == other.B;
72
-
73
- }
74
-
75
- else if (!A.equals(other.A))return false;
76
-
77
- if (B == null) {
78
-
79
- if (other.B != null)return false;
80
-
81
- }
82
-
83
- else if (!B.equals(other.B))return false;
84
-
85
- return true;
86
70
 
87
71
  }
88
72
 
@@ -92,17 +76,7 @@
92
76
 
93
77
  public int hashCode() {
94
78
 
95
- final int prime = 31;
96
-
97
- int result = 1;
98
-
99
- result = prime * result + ((A == null) ? 0 : A.hashCode());
100
-
101
- result = prime * result + ((B == null) ? 0 : B.hashCode());
102
-
103
- return result;
79
+ return Objects.hash(A, B); }
104
-
105
- }
106
80
 
107
81
  }
108
82
 

1

追記

2018/11/03 15:26

投稿

aiueoaiueoaiue
aiueoaiueoaiue

スコア94

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,85 @@
27
27
  [関連する質問](https://teratail.com/questions/155941)
28
28
 
29
29
  試したこと3つ目は上のURLに関連してます。
30
+
31
+ ###クラスのオーバーライド追記
32
+
33
+ ```Java
34
+
35
+ class Eex {
36
+
37
+ public int A;
38
+
39
+ public int B;
40
+
41
+ public Eex(int a, int b){
42
+
43
+ A = a;
44
+
45
+ B = b;
46
+
47
+ }
48
+
49
+ @Override
50
+
51
+ public String toString() {
52
+
53
+ return String.format("%d %d", A, B);
54
+
55
+ }
56
+
57
+ @Override
58
+
59
+ public boolean equals(Object obj) {
60
+
61
+ if (this == obj)return true;
62
+
63
+ if (obj == null)return false;
64
+
65
+ if (!(obj instanceof Eex))return false;
66
+
67
+ Eex other = (Eex) obj;
68
+
69
+ if (A == null) {
70
+
71
+ if (other.A != null)return false;
72
+
73
+ }
74
+
75
+ else if (!A.equals(other.A))return false;
76
+
77
+ if (B == null) {
78
+
79
+ if (other.B != null)return false;
80
+
81
+ }
82
+
83
+ else if (!B.equals(other.B))return false;
84
+
85
+ return true;
86
+
87
+ }
88
+
89
+
90
+
91
+ @Override
92
+
93
+ public int hashCode() {
94
+
95
+ final int prime = 31;
96
+
97
+ int result = 1;
98
+
99
+ result = prime * result + ((A == null) ? 0 : A.hashCode());
100
+
101
+ result = prime * result + ((B == null) ? 0 : B.hashCode());
102
+
103
+ return result;
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ ```