Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从其他类访问变量_Java - Fatal编程技术网

Java 如何从其他类访问变量

Java 如何从其他类访问变量,java,Java,我有三节课,正在试着运行我的程序。我需要从另一个类访问变量。我已经尝试将变量公开,但这似乎仍然不起作用。给出错误的变量位于TetrisPiece类中。变量有:位、列、行、空和网格。如何访问这些变量 俄罗斯方块 这些变量在Tetris类中声明,它们是静态的,因此为了从另一个类访问它们,请使用类名限定它们 例如,在TetrisPiece构造函数中: public TetrisPiece(int type) { this.type = type; this.squares = new

我有三节课,正在试着运行我的程序。我需要从另一个类访问变量。我已经尝试将变量公开,但这似乎仍然不起作用。给出错误的变量位于TetrisPiece类中。变量有:位、列、行、空和网格。如何访问这些变量

俄罗斯方块


这些变量在Tetris类中声明,它们是静态的,因此为了从另一个类访问它们,请使用类名限定它们

例如,在TetrisPiece构造函数中:

public TetrisPiece(int type) {
    this.type = type;
    this.squares = new boolean[4][4];
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            this.squares[i][j] = Tetris.PIECE_BITS[type][i][j];
}

这些变量在Tetris类中声明,它们是静态的,因此为了从另一个类访问它们,请使用类名限定它们

例如,在TetrisPiece构造函数中:

public TetrisPiece(int type) {
    this.type = type;
    this.squares = new boolean[4][4];
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            this.squares[i][j] = Tetris.PIECE_BITS[type][i][j];
}

@Eran的答案是正确的,但如果您不想更改代码,也可以使用

在导入语句中添加以下内容

import static Tetris.PIECE_BITS;
import static Tetris.COLUMNS;
import static Tetris.ROWS;
import static Tetris.EMPTY;
网格变量不是静态的,因此不能使用相同的

其定义为

public int grid[][] = new int[ROWS][COLUMNS];
您需要定义一个getter:

public int[][] getGrid () {
    return grid;
}

并调用getGrid方法访问变量值。

来自@Eran的答案是正确的,但是如果您不想更改代码,也可以使用

在导入语句中添加以下内容

import static Tetris.PIECE_BITS;
import static Tetris.COLUMNS;
import static Tetris.ROWS;
import static Tetris.EMPTY;
网格变量不是静态的,因此不能使用相同的

其定义为

public int grid[][] = new int[ROWS][COLUMNS];
您需要定义一个getter:

public int[][] getGrid () {
    return grid;
}

并调用getGrid方法来访问变量值。

它现在工作了一半,而不是显示错误的变量。错误现在在导入语句中的计时器处。错误表示无法解析符号Tetris@jayoguntino您是否在导入语句中添加了计时器?这不是静态变量,您只能对声明为静态的变量使用静态导入,否则您需要为网格变量定义一个类似getter的getter。当我说Timer时,我的意思是说俄罗斯方块,所以在导入语句中每次提到俄罗斯方块时,错误都位于文件顶部。错误读取无法解析符号“俄罗斯方块”是否在特定包中定义俄罗斯方块?你需要在你的导入中输入完整的包名,比如import static..Tetris.xxxxIt现在工作了一半,而不是显示错误的变量。错误现在在你的导入语句中的计时器上。错误表示无法解析符号Tetris@jayoguntino您是否在导入语句中添加了计时器?这不是静态变量,您只能对声明为静态的变量使用静态导入,否则您需要为网格变量定义一个类似getter的getter。当我说Timer时,我的意思是说俄罗斯方块,所以在导入语句中每次提到俄罗斯方块时,错误都位于文件顶部。错误读取无法解析符号“俄罗斯方块”是否在特定包中定义俄罗斯方块?您需要将完整的包名放入导入中,如导入static..Tetris.xxxx尝试对变量执行相同操作时出错:grid EMPTY和time错误为非静态字段无法从static引用context@jayoguntino考虑到您发布的代码,此错误毫无意义。EMPTY在您发布的代码中是静态的,它也是私有的,如果您想从其他类访问它,应该更改它。可能您正在运行的代码与您发布的代码不同。当我尝试对变量执行相同操作时,会出现一个错误:grid EMPTY and time the error is Non static字段不能从static引用context@jayoguntino考虑到您发布的代码,此错误毫无意义。EMPTY在您发布的代码中是静态的,它也是私有的,如果您想从其他类访问它,应该更改它。也许您正在运行的代码与您发布的代码不同。