2012-03-28 62 views
2

每當我編譯我的代碼,我收到以下錯誤:錯誤:構造函數類Player中的Player不能應用於給定的類型;

error: constructor Player in class Player cannot be applied to given types;

,但它沒有列出任何類型。有問題的代碼是

public class Team { 
private String name; 
public Player players[]; 
public Player temp; 

public Team(String inputname, Player players[]) { 

    inputname = name; 
    this.players = new Player [players.length]; 
    for(int k=0 ; k<players.length ; k++) 
     this.players[k] = new Player(players[k]); //This is the line with errors. 

} 

Player類如下發現:

public class Player { 

public String[] name; 

public Player(String inputname) { 

    name = inputname.split(" "); 

}} 

有人能告訴我這裏有什麼問題?如果有幫助,在出現錯誤的情況下,球員[k]將會是一個像名字一樣的字符串,比如「比利鮑勃」。

回答

2
players[k] 

是類型的球員,和你的構造期待字符串:

public Player(String inputname) 
+0

非常感謝的第k索引的構造的對象!修改我的代碼以從Strings創建Player對象,現在它工作得很好。 – 2012-03-28 23:51:20

2

的錯誤是,播放器的構造函數(public Player(String inputname))需要一個字符串,但你想傳遞一個播放器對象(數組的第k個元素players)。

修復方法是寫this.players[k] = new Player(inputName);

因此通過構造一個字符串,和存儲在所述陣列players

0
class CricketPlayer extends Player 
{ 
    CricketPlayer(String var) 
    { 
    } 

    public void Play() 
    { 
     System.out.println("Play Cricket:-" + getName()); 
    } 
}