gpt4 book ai didi

java - 如何从不同的类访问变量

转载 作者:行者123 更新时间:2023-12-01 13:58:40 24 4
gpt4 key购买 nike

我正在尝试访问 WordSelect 类中的变量,然后尝试在 Game 类中使用它。代码如下:WordSelect 类:

import static java.lang.System.*;
import java.util.*;

public class WordSelect {

public WordSelect(){

}

public void Start(int diff){
String words[] = new String[26];
switch(diff){
case 1:
words[0] = "cat";
words[1] = "dog";
words[2] = "book";
words[3] = "breakfeast";
words[4] = "telephone";
words[5] = "mixture";
words[6] = "music";
words[7] = "animal";
words[8] = "school";
words[9] = "plant";
words[10] = "pen";
words[11] = "pencil";
words[12] = "paper";
words[13] = "note";
words[14] = "fog";
words[15] = "smoke";
words[16] = "bake";
words[17] = "alone";
words[18] = "drive";
words[19] = "town";
words[20] = "city";
words[21] = "sunny";
words[22] = "shine";
words[23] = "polish";
words[24] = "cap";
words[25] = "hat";
break;

case 2:
words[0] = "president";
words[1] = "exclamation";
words[2] = "statement";
words[3] = "television";
words[4] = "physics";
words[5] = "algebra";
words[6] = "geometry";
words[7] = "difficult";
words[8] = "extreme";
words[9] = "procedure";
words[10] = "ship";
words[11] = "soldier";
words[12] = "lunch";
words[13] = "hockey";
words[14] = "tennis";
words[15] = "soccer";
words[16] = "football";
words[17] = "basketball";
words[18] = "bias";
words[19] = "magazine";
words[20] = "computer";
words[21] = "internet";
words[22] = "allegedly";
words[23] = "system";
words[24] = "unison";
words[25] = "excited";
break;

case 3:
words[0] = "amalgamation";
words[1] = "proclomation";
words[2] = "establishment";
words[3] = "rehabilitation";
words[4] = "rhinoceros";
words[5] = "velociraptor";
words[6] = "declaration";
words[7] = "announcement";
words[8] = "binomial";
words[9] = "polynomial";
words[10] = "congregation";
words[11] = "obligation";
words[12] = "structure";
words[13] = "description";
words[14] = "perscription";
words[15] = "subscribe";
words[16] = "address";
words[17] = "township";
words[18] = "mischievous";
words[19] = "bewildered";
words[20] = "accusation";
words[21] = "designation";
words[22] = "disgusting";
words[23] = "prolonged";
words[24] = "restoration";
words[25] = "regeneration";
}

int i = words.length;

Random rng = new Random();
int choice = rng.nextInt(words.length); //Varible storing random word
String wd = words[choice];
// Not sure what to put here to make wd available in the other class
}
}

以及游戏类:

import static java.lang.System.*;
import java.util.*;

public class Game {

public static void game(){
out.println(wd); //Trying to print out the wd variable from WordSelect

}
}

最佳答案

您可以将变量更改为静态变量,因为在您的情况下,我认为wd变量与您的类中的任何对象都不相关。因此,静态变量必须满足您的需要。然后,您只需以Classname.variable的形式使用它即可。

WordSelect 类中:

public class WordSelect {
public static String wd = words[choice];
public WordSelect(){
....
....
}

public void Start(int diff){
....
....
}

然后,在 GameCalss 中:

public class Game {

public static void game(){
system.out.println(WordSelect.wd);

}
}

注意:静态变量应该在任何方法之外定义。

关于java - 如何从不同的类访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19482102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com