简体   繁体   中英

Constructor is already defined in class

I am beginning to learn java, and am coding a simple hockey statistics class. It looks like this:

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';
    }


}

Now, this all compiles fine until I enter the code for the last block. When I try to compile it, I get this error:

(Player.java:52 error: constructor Player(int) is already defined in class Player)

What am I doing wrong? I am following the format of my textbook quite closely to build this class, but I keep running into this error. Can anyone give me a reason why this is happening? Because I do not fully understand this compiler error.

You have not overloaded properly.

You have 2 constructors with same signature.

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

and

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

So one quick solution will be merging the both constructors.

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

Have a single constructor and pass zero instead when the param is not available.

For ex

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

You duplicated the constructor, the compiler can not decide which one of those you want to call when typing new Player(8) :

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

Try calling new Player() from a new set of methods

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

And call it with

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

and

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

Have the same signature Player(int) . So you should change your constructors or delete one.

EDIT (I add more code for clarify)

Method 1:

enum ConstructorType {
 GOAL,
 GAME
}

public Player(int value, ValueType type)
{
    switch(type){
      case GOAL:
goals = value;
        break;
      case GAME:
games = value;
         break;
default:
break;
    }
this(games, goals, 0, 'X'); // Prefer this one instead of repeat your constructor code.
}

Method 2:

public Player(int initialGoals, int initialGames)
{
this(initialGames, initialGoals, 0, 'X');
}

Multiple constructors with the same parameters aren't allowed. When creating multiple constructors have one main constructor build an object with every possible parameter. If an object is allowed to be created with less parameters then create another constructor which calls the main constructor.

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;
}

More constructors can be created to accommodate more parameters using the same format as the first constructor.

You can't define constructors with same parameter type. And in this case, I think you should use your first constructor + setters instead of define many constructors with just one parameter.

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;
   }
}

And then:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM