0

私は Java の初心者で、しばらくの間 C++ と OOP をコーディングしており、私にとってエキサイティングな新しい冒険です。

問題を検索しようとしましたが、答えを個人的な問題に移すことができなかったので、ここに行きます:

ループ.java

public class Loop {
public int x;
public int y;
public int size;

public static void main(String [] args){
    new Loop(4, 4 ,2 );
}
private boolean game;

//---------------------------------------- constructor
public Loop(){
    }
public Loop(int height, int width, int cell_size){
    x = width;
    y = height;
    size = cell_size;
    System.out.println("Loop()");
    game = true;
    new Build_Cells(y,x);
    //run();
}
};

Build_Cells.java

import java.util.*;

public class Build_Cells extends Loop {
private List<List<Cell>> map = new ArrayList<List<Cell>>();
public int col;
public int rows;
public void printMap(){
    System.out.println("PrintMap()");
    for( int i = 0; i < map.size() /** col */; i++){
        for( int u = 0; u < map.get(i).size() /** rows */ ;u++){
            System.out.print(map.get(i).get(u).getState());
        }
        System.out.println();
    }
}
public Cell getCell(int a, int b){
    return map.get(a).get(b);
}
//---------------------------------------- constructor
public Build_Cells(){
}
public Build_Cells( int by, int bx){
    System.out.println("Build_Cells()");
    col = by;
    rows = bx;
    for( int i = 0; i < col ; i++){
        List<Cell> colObj = new ArrayList<Cell>(rows);
        map.add(y, colObj);
        for(int u = 0; u < rows; u++){
            colObj.add( new Cell() );
        }
    }
    printMap();
}
};

Cell.java

public class Cell extends Build_Cells {
private int state;
private int nemesis;
private int next;
private int getNem(int cs){
    int cata;
    if(cs == 1)
        cata = 0;
    else if(cs == (0 | 2 | 3) )
        cata = 1;
    else
        cata = 6;
    return cata;
}


//---------------------------------------- constructor
public Cell(){
    System.out.println("Cell()");
    set_state(5);
}
public void set_state(int input){
    state = input;
    nemesis = getNem(state);
}
public int getState(){
    return state;
}
};

Build_CellsgetCell()関数と とのCell関数を使用できるようにするにはどうすればよいですか?getState()setState()Loop

4

1 に答える 1

0

Build_Cell参照を保存せずに のインスタンスを作成しました。変更する場合:

new Build_Cells(y,x);

Build_Cells buildCells = new Build_Cells(y,x);

buildCells.getCell()から電話をかけることができますLoop。を呼び出してセルの状態を取得したり、 を呼び出してセルの状態をbuildCells.getCell().getState()設定したりできますbuildCells.getCell().setState()

また、そこから機能を使用しないため、Cellおそらく拡張すべきではありません。Build_CellsJava での OOP の詳細については、こちらを参照してください。

于 2013-03-19T15:32:04.683 に答える