error: constructor Player in class Player cannot b

2019-07-20 04:45发布

whenever I compile my code, I receive the following errors:

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

but it doesn't list any types. The code in question is

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.

}

The Player class is found below:

public class Player {

public String[] name;

public Player(String inputname) {

    name = inputname.split(" ");

}}

Can someone please tell me what is wrong here? If it helps, on the line with errors, players[k] would be a name-like string, such as "Billy Bob."

3条回答
走好不送
2楼-- · 2019-07-20 05:12
players[k] 

is of type Player, and your constructor is expecting String:

public Player(String inputname) 
查看更多
趁早两清
3楼-- · 2019-07-20 05:20

The error is that the constructor of Player (public Player(String inputname)) expects a string, but you are trying to pass it a Player object (the k'th element of the array players).

The fix is to write this.players[k] = new Player(inputName);

Thus passing the constructor a string, and storing the constructed object in the k'th index of the array players

查看更多
该账号已被封号
4楼-- · 2019-07-20 05:26
class CricketPlayer extends Player
{
    CricketPlayer(String var)
    {
    }

    public void Play()
    {
        System.out.println("Play Cricket:-" + getName());
    }
}
查看更多
登录 后发表回答