简体   繁体   中英

Java. Implicit super constructor Settore() is undefined. must explicitly invoke another constructor

When I've Created the subclass Alieni from the Settore class I get the error "Implicit super constructor Settore() is undefined. must explicitly invoke another constructor", i've looked similar questions and the answer given was to put a default constructor in my Settore class, i've done it and still get the same error

public class Settore {
            private Nome settoreNome;
            private char letteraX;
            private final int coordinataX;
            private final int coordinataY;
            private int giocatoriPresenti;


public static void main(String[] args) {
    // TODO Auto-generated method stub

}
public int getGiocatoriPresenti() {
    return giocatoriPresenti;
}
public void setGiocatoriPresenti(int giocatoriPresenti) {
    this.giocatoriPresenti = giocatoriPresenti;
}
public char getLetteraX() {
    return letteraX;
}

public void setLetteraX(char letteraX){
    this.letteraX = letteraX;
}

public Settore (){}//suggestion given, still doesn't fix the problem, it just makes it worse
public Settore (int coordinataX, int coordinataY){
        char myChar =letteraX;
        int i=(int)myChar;
        this.coordinataX=i-97;
        this.coordinataY=coordinataY-1;
    }
    public int getX(){
        return coordinataX;
    }
    public int getY(){
        return coordinataY;
    }
    public Nome getSettoreNome() {
        return settoreNome;
    }
    public void setSettoreNome(Nome settoreNome) {
        this.settoreNome = settoreNome;
    }
    }
    public enum Nome {
    SICURO, PERICOLOSO, SCIALUPPA, ALIENI, UMANI
    }


    public class Alieni extends Settore {

        public Alieni() {//this is where i get the error Implicit super constructor Settore() is undefined. must explicitly invoke another constructor

            setSettoreNome(Nome.ALIENI);
        }

    }

It shows error even when you add a default constructor, bcoz the final variables you declared should have some value.They should be assigned values in the default constructor as below:

 public Settore (){
    coordinataX=5;
    coordinataY=22;
  }

Another way, if you want to use the parameterized constructor you have declared:

          public Alieni() 
          {
                super(5,6);  //call to super class constructor
                setSettoreNome(Nome.ALIENI);
          }

You needed to add a default constructor because you need to initialize coordinataX and coordinataY :

public Settore(){
    coordinataX=1;
    coordinataY=2;
}

Otherwise the compiler will complain that these may not be initialized because a final member variable needs to be initialized at the declaration or in the constructor.

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