我創建了我的類播放器的2個實例,但似乎創建的最後一個實例總是接管(重疊?)所有其他對象,這裏有什麼問題? (我是初學者,當談到編程)Java中的重疊對象
下面是程序:
public class Test
{
public static void main (String[] args)
{
Player player1 = new Player("Player 1");
Player player2 = new Player("Player 2");
Player player3 = new Player("Player 3");
System.out.println(player1.getName());
}
}
這是輸出
Player 3
這是類
import java.util.Scanner;
import java.util.Random;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Player
{
public static String name;
public static int score;
public static Die[] dice = new Die[5];
public static JRadioButton[] rerollButton = new JRadioButton[5];
//----------------------------------------------------------------
// Constructor - initialize values
//----------------------------------------------------------------
public Player(String n)
{
name = n;
score = 0;
// initialize all dice and rerollButtons in their respective arrays
for (int i = 0; i < 5; i++) {
dice[i] = new Die();
rerollButton[i] = new JRadioButton();
}
}
public String getName()
{
return name;
}
}
我試圖尋找其他類似的問題,但我發現每一個問題都很複雜,我真的明白。任何幫助將不勝感激。
私人並非越大越好。 – 2013-11-27 04:45:21
但我認爲這可能是在定義了公共getter的情況下 – craiglm