简体   繁体   English

Java。 定义方法时遇到麻烦

[英]Java. Trouble defining a method

I have the class Mappa which has a method of the same name which creates two matrix 我有Mappa类,该类具有创建两个矩阵的同名方法

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 然后我有MappaCreator类(在另一个包中),此类的主要功能是调用Mappa类的mappa()方法,当我像这样直接在类内部编写时,没有问题

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 但是当我在MappaCreator()方法中写入它时,得到警告“未使用局部变量的值”,并且在子类MappaFermiCreator()的方法MappaFermiCreator()中出现错误“ mappa无法解析或不是字段”

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. MappaCreator或其子类MappaFermiCreator都没有实例变量mappa因此无法编译。 Add this to the superclass MappaCreator 将此添加到超类MappaCreator

protected Mappa mappa;

or add this to MappaFermiCreator 或将其添加到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 mappa是此方法(构造函数)中的局部变量,由于这是唯一的行,因此从不使用该变量


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. this.mappa从未声明,因此无法解析。


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. 避免这种情况的一种方法是采用super()以便使用在超类中定义的功能。

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 . 这假定MappaFermi extends Mappa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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