繁体   English   中英

从静态调用非静态方法(在不同的类中)

[英]Calling non-static method (in a different class) from static

我已经在这里四处寻找解决方案,但是找不到。 我尝试了这些以及其他许多方法,但遇到了同样的问题。

我试图制作一个简单的文字游戏,但遇到一个问题,我有一个主类和一个名为“ gameboard”的类,该类作为数组定义如下:

static GameBoard[] gameboard = new GameBoard[9];

现在,这很好用,直到我尝试更改这些数组对象中单个对象的特征为止。 我会做:

gameboard[input].setState(2);

并且应该更改的游戏板的特定实例将不是唯一的实例:当我这样做时,它们都将更改。 有点奇怪。 仅应更改gameboard[**input**] ,而不更改所有9个实例。 我拥有的每个变量和方法都是“静态”的,但是由于main方法(public static void main ...),所有内容似乎都必须是静态的。 我如何摆脱所有这些静电?

游戏板类

package com.name.tictactoe;

public class GameBoard {
 char[] States = {'N','X','O'};
 char state;

public  void setState(int s){
    state = States[s];
}
public  char getState(){
    return state;
}
}

主班(称为游戏)

package com.name.tictactoe;

import java.util.Scanner;

public class Game {

static boolean turn, win;
static GameBoard[] gameboard;
static Scanner kb = new Scanner(System.in);
static int input;

public static void main(String[] args){
    gameboard = new GameBoard[9];
    reset();
    displayStates();
    askTurn();
    displayStates();
    askTurn();

}

public static void askTurn() {
    System.out.println();
    System.out.println();
    System.out.println("Where do you want to go?  Use the numbers shown, where the first segment is the top and the last is the bottom - left to right.");
    input = kb.nextInt();
    if(input > 8){
        System.out.println("Input out of bounds.  Game over by default.");
        try {
            Thread.sleep(1000000000);} catch (InterruptedException e) {e.printStackTrace();}
    }
    gameboard[input].setState(2);
}

public static void reset(){
    for(int i = 0; i < 9; i++){
        gameboard[i].setState(0);
    }
}

public static void displayStates(){
    for(int i = 0; i < 9; i++){
        System.out.print(gameboard[i].getState() + " ");
        if(i ==2 || i ==5){
            System.out.print("  II  ");
        }
    }
    System.out.println();
    for(int i = 0; i < 9; i++){
        System.out.print(i + " ");
        if(i ==2 || i ==5){
            System.out.print("  II  ");
        }
    }
    System.out.println();
}

}

更新:当前答案不起作用。 尽管Eclipse并没有意识到这一点,但使GameBoard成为非静态的会导致在引用其中的任何方法时导致空指针异常。

static变量属于 ,而不是对象,因此,您所有的GameBoard都将受到影响!

仅仅因为您使用静态方法并不意味着您无法操纵实例变量。 首先,使GameBoard类中的所有内容均为非静态(除非您确实确实需要在所有实例之间共享的某些值)。 这包括实例变量和getter / setter方法。

如果您的程序仅通过main方法运行,则将GameBoard[]对象保持静态。 然后,当您调用该方法时:

gameboard[input].setState(2);

这只会更改索引inputGameBoard的状态。

编辑:

要在其中包含基本GameBoard对象的情况下实例化GameBoard[] ,可以在main方法的开头进行此操作:

for(int x=0; x<gameboard.length; x++) {
    gameboard[x] = new GameBoard(); //Assuming you want to use the default constructor
}

数组中的其他对象不应更改。 您可以添加更多代码以获得更多上下文吗?

据我了解,您正在执行以下操作:

public class Main {
    public static String[] strings = new String[2];

    public static void main(String[] args) {
        strings[0] = "test";
        System.out.println(strings[1]);
    }
}

在我的示例中,输出为预期的“空”。

我如何摆脱所有这些静电?

只需在main函数中创建对象的实例。

GameBoard[] gameboard = new GameBoard[9];

对于您描述要发生的事情,必须将游戏板数组的所有元素都设置为同一对象(与静态数组无关),检查用GameBoard类的新实例填充gameBoard数组的代码是否存在错误这可能导致将同一实例写入所有元素(或在此处发布该代码,以便人们可以看到问题)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM