紙牌游戲運行異常
懇請各位老師和大神的指點,謝謝!
我的紙牌代碼總是報錯:Exception in thread "main" java.lang.StackOverflowError
at CardsGame.Player.<init>(Player.java:20) 后面重復很多遍這個
錯誤指向源代碼如下(創建兩名玩家那里,加粗加下劃線了):
public class Player {
String id;
String name;
public Set<Cards> twoCards ;//= new HashSet<Cards>();
public Player(){};
public Player(String id,String name){
this.id = id;
this.name = name;
this.twoCards = new HashSet<Cards>();
}
//創建兩名玩家
public Player player1 = new Player("1","wangxin");
public Player player2 = new Player("2","zhangxu");
public void TwoPlayer(){
System.out.println("兩名玩家創建完成:");
System.out.println(player1.id +":" +player1.name +" ?" +player2.id +":" +player2.name);
}
}