构造函数已在类中定义

时间:2018-02-06 04:03:47

标签: java

我开始学习java,并正在编写一个简单的曲棍球统计课程。它看起来像这样:

public class Player 
{
    private int games; 
    private int goals;
    private int assists;
    private char position;


    public Player()
    {
        games = 0;
        goals = 0;
        assists = 0;
        position = 'X';
    }


    public Player(int initialGames, int initialGoals, int initialAssists, 
    char initialPosition )

    {
        games = initialGames;
        goals = initialGoals;
        assists = initialAssists;
        position = initialPosition;

    } 

    public void setPlayer(int newGames, int newGoals, int newAssists, char 
newPosition)
    {
        games = newGames;
        goals = newGoals;
        assists = newAssists;
        position = newPosition;

    } 

    public Player(int initialGames)
    {
        games = initialGames;
        goals = 0;
        assists = 0;
        position = 'X';
    }

    public void setGames(int newGames)
    {
        games = newGames;
    }

    public Player(int initialGoals)
    {
        games = 0;
        goals = initialGoals;
        assists = 0;
        position = 'X';
    }


}

现在,这一切都很好,直到我输入最后一个块的代码。当我尝试编译它时,我收到此错误:

(Player.java:52错误:构造函数Player(int)已在类Player中定义)

我做错了什么?我非常密切地遵循我的教科书的格式来构建这个类,但我一直在遇到这个错误。任何人都可以告诉我为什么会这样吗?因为我不完全理解这个编译错误。

5 个答案:

答案 0 :(得分:4)

您没有正确超载。

你有2个具有相同签名的构造函数。

public Player(int initialGoals)
    {
        games = 0;
        goals = initialGoals;
        assists = 0;
        position = 'X';
    }

public Player(int initialGames)
    {
        games = initialGames;
        goals = 0;
        assists = 0;
        position = 'X';
    }

因此,一个快速的解决方案是合并两个构造函数。

public Player(int initialGoals, int initialGames)
    {
        games = initialGames;
        goals = initialGoals;
        assists = 0;
        position = 'X';
    }

当param不可用时,有一个构造函数并传递零。

前者

Player p = new Player(5,0); // games 0
Player p = new Player(0,5); // goals 0

答案 1 :(得分:0)

您复制了构造函数,编译器在键入setTimeout时无法决定您要调用哪一个:

setTimeout

尝试从一组新方法中调用new Player(8)

public Player(int initialGoals)
public Player(int initialGames)

并用

调用它
new Player()

答案 2 :(得分:0)

static Player NewPlayerFromGoals(int initialGoals){...}
static Player NewPlayerFromGames(int initialGames){...}

Player p = Player.NewPlayerFromGoals(8);

拥有相同的签名public Player(int initialGoals) { games = 0; goals = initialGoals; assists = 0; position = 'X'; } 。 因此,您应该更改构造函数或删除构造函数。

编辑(我添加更多代码以澄清)

方法1:

public Player(int initialGames)
{
    games = initialGames;
    goals = 0;
    assists = 0;
    position = 'X';
}

方法2:

Player(int)

答案 3 :(得分:0)

不允许使用具有相同参数的多个构造函数。当创建多个构造函数时,一个主构造函数构建一个包含每个可能参数的对象。如果允许使用较少的参数创建对象,则创建另一个调用主构造函数的构造函数。

public Player(int games){
    this(games, 0, 0, 0);
}
public Player(int games, int goals, int assists, char position){
    this.games = games;
    this.goals = goals;
    this.assists = assists;
    this.position = position;
}

可以使用与第一个构造函数相同的格式创建更多构造函数以容纳更多参数。

答案 4 :(得分:0)

您无法定义具有相同参数类型的构造函数。在这种情况下,我认为你应该使用你的第一个构造函数+ setter,而不是只用一个参数定义许多构造函数。

public class Player  {
   private int games; 
   private int goals;
   private int assists;
   private char position;

   public Player() {
    games = 0;
    goals = 0;
    assists = 0;
    position = 'X';
   }

   public Player(int initialGames, int initialGoals, int initialAssists, 
   char initialPosition ) {
       games = initialGames;
       goals = initialGoals;
       assists = initialAssists;
       position = initialPosition;
   }

   // SETTER
   public void setGames (int games) {
      this.games = games;
   }

   public void setGoals (int goals) {
      this.goals= goals;
   }
}

然后:

Player playerA = new Player();
playerA.setGames(1);

Player playerB = new Player();
playerB.setGoals(2);