I have the class Mappa which has a method of the same name which creates two matrix
package mappa.product;
import mappa.Settore;
public class Mappa {
private Name mappaName;
private final Settore [][] settore;
private int Matrice [][];
private static final int X=23;
private static final int Y=14;
public Mappa (){
settore = new Settore[X][Y];
for (int i=0; i < X; i++){
for (int j=0; j<Y; j++) {
settore[i][j] = new Settore (i,j);
}
}
Matrice = new int[23][14];
}
public int[][] getMatrice() {
return Matrice;
}
public void setMatrice(int matrice[][]) {
Matrice = matrice;
}
public Name getMappaName() {
return mappaName;
}
public void setMappaName(Name mappaName) {
this.mappaName = mappaName;
}
public Settore[][] getSettori(){
return settore;
}
public void addSettori(){
Settore.add(settore);
}
}
then i have the class MappaCreator (which is in another package) and the mainly function of this class is to recall the method mappa() of the class Mappa, when i write directly inside the class like this there is no problem
package mappa.creator;
import mappa.product.*;
public class MappaCreator {
Mappa mappa = new Mappa();
public MappaCreator(){
}
}
package mappa.creator;
import mappa.product.*;
public class MappaFermiCreator extends MappaCreator {
public MappaFermiCreator() {
Mappa mappa = new MappaFermi();
this.mappa=mappa;
}
}
but when i write it inside the method MappaCreator() i get the warning the "value of the local variable is not used" and the error "mappa cannot be resolved or is not a field" in the method MappaFermiCreator() of the subclass MappaFermiCreator
package mappa.creator;
import mappa.product.*;
public class MappaCreator {
public MappaCreator(){
Mappa mappa = new Mappa();//The value of the local variable is not used
}
}
package mappa.creator;
import mappa.product.*;
public class MappaFermiCreator extends MappaCreator {
public MappaFermiCreator() {
Mappa mappa = new MappaFermi();
this.mappa=mappa;//mappa cannot be resolved or is not a field
}
}
Neither MappaCreator
nor its subclass MappaFermiCreator
has an instance variable mappa
so it doesn't compile. Add this to the superclass MappaCreator
protected Mappa mappa;
or add this to MappaFermiCreator
private Mappa mappa;
public MappaCreator(){
Mappa mappa = new Mappa();//The value of the local variable is not used
}
mappa
is a local variable in this method (the constructor) and as this is the only line, the variable is never used
public MappaFermiCreator() {
Mappa mappa = new MappaFermi();
this.mappa=mappa;//mappa cannot be resolved or is not a field
}
this.mappa
is never declared and therefore cannot be resolved.
You should not duplicate the logic of the super class in the sub class. One way to avoid this is to employ super()
in order to use the functionality that is defined in the super class.
public class MappaCreator
{
private Mappa mappa;
public MappaCreator(Mappa mappa)
{
this.mappa = mappa;
}
public MappaCreator()
{
this(new Mappa());
}
}
public class MappaFermiCreator extends MappaCreator
{
public MappaFermiCreator()
{
super(new MappaFermi());
}
}
This assumes that MappaFermi extends Mappa
.
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.