- public class GamePlayer {
- public static String playername;
- public GamePlayer(String name) {
- playername = name;
- }
- public String toString() {
- return "[player:" + playername + "]";
- }
- public static void main(String[] args) {
- GamePlayer[] player = new GamePlayer[3];
- player[0] = new GamePlayer("Mad Hatter");
- player[1] = new GamePlayer("March Hare");
- player[2] = new GamePlayer("Alice");
- for (int i = 0; i < player.length; i++) {
- System.out.println(player[i]);
- }
- }
- }
复制代码
期望输出结果:
[player:Mad Hatter]
[player:March Hare]
[player:Alice]
实际输出结果:
[player:Alice]
[player:Alice]
[player:Alice]
错在哪? |