簡體   English   中英

在Java中的類之間共享變量

[英]Sharing Variables between classes in Java

我必須在課堂上創建一個迷宮游戲。 我已經創建了迷宮,但是我需要共享從主類到其他類的變量(這是必需的)。

這是我的主要代碼:

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 '");

        }

    }
}

我正在嘗試將變量行,列和大小共享到我的Location類中:

import java.io.*;
import java.util.Scanner;

public class Location 
{

    MazeGame Location = new MazeGame();


    public Location()
    {
        String Wall;
        String Space;
        String Endpoint;
        Boolean Visited;
        Boolean hereNow;   
    }

    public void getVariables()
    {
        Location.getVariables();
        if(row == 0)
        {

        }

    }



}

預先感謝您的任何幫助。

public class Location 
{

MazeGame Location = new MazeGame();
....

這不會起作用,因為類名與變量名完全相同。

另外,從不使用您的Location類,因為在MazeGame類中聲明了程序入口點(main函數)。

我在其他地方寫了另一個答案 ,解釋了您可能想閱讀的構造函數,類和設置器/獲取器。

讓我知道您是否需要更多示例,或者仍然不了解。

正如其他人指出的那樣,類名“ Location”與變量(對象)名相同。 您需要閱讀有關OOP規則和原則的信息。

跨類共享變量的一種方法是將變量聲明為“靜態”。 請記住,不鼓勵使用“靜態”變量,它們被認為是邪惡的。

不過,如果您希望了解代碼如何在類之間共享變量,請在此處進行一些更改(盡管這並不完美)。

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);
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM