回答編集履歴

2

リンクなどを追記

2018/02/28 12:13

投稿

aterai
aterai

スコア158

test CHANGED
@@ -1 +1,29 @@
1
- すでに回答済みなので削除
1
+ ~~すでに回答済みなので削除~~
2
+
3
+
4
+
5
+ 変数`n`に`static`修飾子が付いて、`Card`クラスのクラス変数(静的フィールド)になっています。
6
+
7
+ このため、すべての`Card`のインスタンスで値が共有され、最後に設定されたハートの`13`が`Card.n`の値となります。各`Card`オブジェクト固有の値を持つように`n`をクラス変数ではなくインスタンス変数に変更すべきです。
8
+
9
+ ```java
10
+
11
+ // private static int n;
12
+
13
+ private int n;
14
+
15
+ public Card (int n) {
16
+
17
+ // Card.n = n;
18
+
19
+ this.n = n;
20
+
21
+ }
22
+
23
+ ```
24
+
25
+ 追記:
26
+
27
+ - [Understanding Class Members (The Java™ Tutorials>Learning the Java Language>Classes and Objects)](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html)
28
+
29
+ - `static`修飾子については、上記のJavaチュートリアルにある`Bicycle`クラスの例が参考になりそうです。

1

すでに回答済みなので削除

2018/02/28 12:13

投稿

aterai
aterai

スコア158

test CHANGED
@@ -1,21 +1 @@
1
- `n`に`static`修飾子が付いて、`Card`クラスのクラス変数(静的フィールド)になっています。
2
-
3
- このため、すべての`Card`のインスタンスで値が共有され、最後に設定されたハートの`13`が`Card.n`の値となります。各`Card`オブジェクト固有の値を持つように`n`をクラス変数ではなくインスタンス変数に変更すべきです。
4
-
5
-
6
-
7
- ```java
8
-
9
- // private static int n;
10
-
11
- private int n;
12
-
13
- public Card (int n) {
14
-
15
- // Card.n = n;
16
-
17
- this.n = n;
1
+ すでに回答済みなので削除
18
-
19
- }
20
-
21
- ```