黑马程序员技术交流社区
标题:
下面的编码哪里出了问题?
[打印本页]
作者:
丶小天
时间:
2014-2-24 15:49
标题:
下面的编码哪里出了问题?
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]
错在哪?
作者:
长石
时间:
2014-2-24 16:00
哥们你把playername设置成静态的 当然所有的对象都一个playername了
去掉static就哦了
作者:
asvsass
时间:
2014-2-24 16:00
public static String playername;
把成员变量的静态去掉!!!
作者:
kira
时间:
2014-2-24 16:01
把静态去掉
public class GamePlayer {
public 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]);
}
}
}
复制代码
作者:
张东健
时间:
2014-2-24 16:02
去掉静态修饰符
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2