문제

How do you access inherited attributes in java outside the constructor?

public PainelPrincipal(Jogo jogo,GridPanel gridPanelPrincipal) {
    super(jogo,gridPanelPrincipal);
    listaBlocos = new LinkedList<>();

    carregarFicheiroNivel();
    gridPanelPrincipal.setEventHandler(this);
}

private void carregarFicheiroNivel() {
    FileHandler handler = new FileHandler("/niveis/EstadoInicial.txt");
    String conteudo = handler.readFile();
    String[] colunas = null;

    int y=0;
    for(String linha: conteudo.split("\n")){
        colunas = linha.split(" ");
        for(int x = 0; x < colunas.length; x++) {
            if(colunas[x].substring(1, 2).equals(PAREDE)){
                grelha[x][y] = new Parede();
                gridPanelPrincipal.put(0, 0, grelha[0][0].getCellRepresentation());
            }else{
                switch(colunas[x].substring(0, 1)){
                    case "0":break;
                }
            }
        }
        y++;
    }
}

This line gridPanelPrincipal.put(0, 0, grelha[0][0].getCellRepresentation()); doesn't seem to work, he doesn't recognize the gridPanelPrincipal variable outside the constructor of the class.

Is it possible to access it outside the constructor or how can i do this?

도움이 되었습니까?

해결책

You can't access it outside the constructor, because presumably it's private. You can only access it in the constructor because it's an argument to the constructor.

You can either:

  1. Make the proper variable protected in the superclass to make it visible to the subclass, OR
  2. (Kind of kludgy) Create your own variable in your subclass to store its own reference to the GridPanel, so you can access it in other methods.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top