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

≡放荡痞女 提交于 2019-12-11 02:19:36

问题


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."


回答1:


players[k] 

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

public Player(String inputname) 



回答2:


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




回答3:


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

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


来源:https://stackoverflow.com/questions/9916615/error-constructor-player-in-class-player-cannot-be-applied-to-given-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!