Java中类之间共享变量
我必须在班上做一个迷宫游戏。我已经创建了迷宫,但是我需要共享从主类到其他类的变量(这是一项要求) 这是我的主要代码:Java中类之间共享变量,java,Java,我必须在班上做一个迷宫游戏。我已经创建了迷宫,但是我需要共享从主类到其他类的变量(这是一项要求) 这是我的主要代码: import java.io.*; import java.util.Scanner; public class MazeGame { public static void main(String[] args) { getVariables(); } static void getVariables() {
import java.io.*;
import java.util.Scanner;
public class MazeGame
{
public static void main(String[] args)
{
getVariables();
}
static void getVariables()
{
String fileName = "C:\\Users\\baileyjstewart\\Documents\\NetBeansProjects\\MazeGame\\src\\Maze.input";
int size;
int row;
int column;
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
Scanner s = new Scanner(new File("C:\\Users\\baileyjstewart\\Documents\\NetBeansProjects\\MazeGame\\src\\Maze.input"));
int[][] array = new int[size = s.nextInt()][size];
for (row = 0; row < size; row++)
{
for(column = 0; column < size; column++)
{
array[row][column] = s.nextInt();
if(array[row][column] == 0)
{
System.out.print(" ");
//System.out.print(array [row][column] + " ");
}
else if(array[row][column] == 1)
{
System.out.print("X ");
}
else if(array[row][column] == 2)
{
System.out.print("E ");
}
}
System.out.println(" ");
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '");
}
}
}
提前感谢您的帮助
public class Location
{
MazeGame Location = new MazeGame();
....
这不起作用,因为类名与变量名完全相同
此外,由于程序入口点(主函数)是在MazeGame类中声明的,因此永远不会使用Location类
我写了这篇文章来解释您可能想要阅读的构造函数、类和setter/getter
如果您需要更多的示例或者仍然不理解,请告诉我。正如其他人所指出的,类名“Location”与变量(对象)名相同。您需要阅读OOP规则和原则 跨类共享变量的一种方法是将变量声明为“static”。请记住,不鼓励使用“静态”变量,它们被认为是有害的 不过,如果您想了解代码如何跨类共享变量,下面是您的代码,其中有一些更改(尽管这并不完美)
您确实应该在
位置
中重命名mazeGame实例,并且应该遵循命名标准。对于您的问题,一种解决方案是“升级”行
、列
和大小
到类字段,并将getter添加到MazeGame
(然后您可以在位置
)中使用这些getter。您需要阅读一本关于基本面向对象原则的教程MazeGame Location=new MazeGame()代码>变量名不能与类名相同可能重复
public class Location
{
MazeGame Location = new MazeGame();
....
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MazeGame
{
protected static int row;
public static void main(String[] args) throws IOException
{
getVariables();
}
public int getRow() {
return MazeGame.row;
}
static void getVariables() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter a value for row : ");
String strRow = br.readLine();
MazeGame.row = Integer.parseInt(strRow);
Location ln = new Location();
ln.getVariables();
}
}
import java.io.IOException;
public class Location
{
MazeGame Locationx = new MazeGame();
public Location()
{
}
public void getVariables() throws IOException
{
System.out.println("The value of class variable row, initially is: "+MazeGame.row); // class variable
Locationx.row = 21; // object variable
System.out.println("The value of object variable row, after modification is: "+Locationx.row);
MazeGame.row = 23; // class variable
System.out.println("The value of class variable row, after modification is: "+MazeGame.row);
}
}