Problem with ENUM in JAVA

1

Good the problem that I am having is that basically the program consists of a team of basketball where each player has a certain position in which he plays, for it I defined a enum , the theme is that now I want to use it in the constructor but I can not.

I do not know how to assign each position to each player I create.

public class Jugador{
  private long cedula;
  private String nombre;
  private int edad;
  private int aniosExperiencia;
  enum posicion{BASE,ESCOLTA,ALERO,ALA_PIVOT,PIVOT};

  public Jugador(String nombre, long cedula , int edad){
      this.nombre=nombre;
      this.cedula=cedula;
      this.edad=edad;
  }

  public Jugador(String nombre, long cedula , int edad, int aniosExperiencia){
    this.nombre=nombre;
    this.cedula=cedula;
    this.edad=edad;
    this.aniosExperiencia=aniosExperiencia;
  }

  public String getNombre(){
    return nombre;
  }

  public int getEdad(){
    return edad;
  } 

  public long getCedula(){
    return cedula;
  }

  public void dimeNombre(String nombre){
    this.nombre=nombre;
  }

  public void dimeEdad(int edad){
    this.edad=edad;
  }

  public void dimeCedula(long cedula){
    this.cedula=cedula;
  }
}
    
asked by Floppy 12.12.2018 в 14:28
source

1 answer

1

A enum is nothing more than a proper definition of a variable type.

Once you define the same, it is used like any other variable.

The advantage that it has is that the values it can take are already predefined, and therefore facilitate its use.

To use it, once defined, you have to define a variable of that type:

posicion PosicionJugador;

And in your constructor, pass a variable of the type of enum :

public Jugador(String nombre, long cedula , int edad, int aniosExperiencia, posicion pos){
    this.nombre=nombre;
    this.cedula=cedula;
    this.edad=edad;
    this.aniosExperiencia=aniosExperiencia;
    this.PosicionJugador = pos;
}

But there is something to keep in mind. If your enum is defined within the class, no one outside the class will see it. In general, the enum are defined in another class, or outside of them, in order to be used everywhere.

For more information see here

    
answered by 12.12.2018 / 14:42
source