Java doubt working with superclasses and subclasses

0

A question regarding a job I'm doing in java. It turns out that I have the next part of a superclass:

public abstract class Jugador {
    private String nombre;
    private int cantVidas;
    private int nivelActual;

    public Jugador(String nombre) {
        this.nombre = nombre;
        cantVidas=3;
        nivelActual=0;
    }

There are also its getters and setters corresponding to each variable. I have a subclass called "Beginner", my question is: Can I do the following method in this subclass?

public void pasarDeNivel(){
    int nivel;
    nivel = getNivelActual();
    nivel = nivel + 1;
}

and then make a method that prints the level. and that it fits you that the level is now 1.

I have the problem that I will always have the variable "level" in 0, it never increases to 1.

    
asked by Janzek 27.05.2018 в 00:31
source

1 answer

1

Your problem is that nivel is a local variable, what you should change the variable nivelActual of the class Jugador with your modifier method. your method would be like this:

public void pasarDeNivel(){
    setNivelActual(getNivelActual()+1); //le pasas el nivel actual + 1
}
    
answered by 27.05.2018 в 03:31