我在这里寻找答案(Java: how to "restart" a static class?),但它似乎没有回答我的问题。
我有一个类,它从单词列表中生成一个有点随机的字符串。 然后通过read()将单词输出到游戏中的标志上。 我可以用两种方式做事:如果buildingText(),verbText()和nounText()不是静态的,每次访问该符号时都会得到一个新的字符串。 如果这三个是静态的,即使重新开始游戏,我也会得到一个不会改变的字符串。
如何更改此代码以便reset()创建一个新的随机字符串?
我一直试图使用布尔值并用if包围switch语句,但这似乎不可能。
以下是代码:
public class Sign {
private static final String TXT_PLACE_NAME = buildingText();
private static final String TXT_VERBING = verbText();
private static final String TXT_NOUN = nounText();
protected static final String SIGNS =
"Welcome to the "+ "\n \n "+ TXT_PLACE_NAME+ " of the "+ TXT_VERBING + TXT_NOUN + "";
public void reset(){
//still working on this. As it stands, will only reset if you exit the program
//could be worse things
}
public void read(int pos) {
GameScene.show(new WndMessage(SIGNS));
}
public static String buildingText() {
String buildingType;
switch (Random.Int(1, 4)) {
case 1:
buildingType = "Inn";
break;
case 2:
buildingType = "Inn";
break;
case 3:
buildingType = "Tavern";
break;
case 4:
buildingType = "Pub";
break;
default:
buildingType = "Inn";
break;
}
return buildingType;
}
public static String verbText()
{
String verbName;
switch (Random.Int(1,20))
{
case 1: verbName = " Dancing";
break;
case 2: verbName = " Prancing";
break;
case 3: verbName = " Eating";
break;
case 4: verbName = " Jigging";
break;
case 5: verbName = " Digging";
break;
case 6: verbName = " Flogging";
break;
case 7: verbName = " Floating";
break;
case 8: verbName = " Flying";
break;
case 9: verbName = " Laughing";
break;
case 10: verbName = " Smiling";
break;
case 11: verbName = " Drowning";
break;
case 12: verbName = " Golden";
break;
case 13: verbName = " Silver";
break;
case 14: verbName = " Copper";
break;
case 15: verbName = " Farming";
break;
case 16: verbName = " Running";
break;
case 17: verbName = " Sewing";
break;
case 18: verbName = " Black";
break;
case 19: verbName = " White";
break;
case 20: verbName = " Fighting";
break;
default: verbName = " Gesticulating";
break;
}
return verbName;
}
public static String nounText()
{
String nounName;
switch (Random.Int(1,20))
{
case 1: nounName = " Pony";
break;
case 2: nounName = " Horse";
break;
case 3: nounName = " Griffin";
break;
case 4: nounName = " Dragon";
break;
case 5: nounName = " Wench";
break;
case 6: nounName = " Bastard";
break;
case 7: nounName = " Ogre";
break;
case 8: nounName = " Troll";
break;
case 9: nounName = " Ox";
break;
case 10: nounName = " Cow";
break;
case 11: nounName = " Cock";
break;
case 12: nounName = " Hen";
break;
case 13: nounName = " Ram";
break;
case 14: nounName = " Ewe";
break;
case 15: nounName = " Dog";
break;
case 16: nounName = " Merchant";
break;
case 17: nounName = " Fisherman";
break;
case 18: nounName = " Arborist";
break;
case 19: nounName = " Archer";
break;
case 20: nounName = " Gallbladder";
break;
default: nounName = " Pancreas";
break;
}
return nounName;
}
}
编辑:
这是'最好的'解。它结合了以下两种解决方案的元素,并产生了预期的结果。请随意批评格式和组织!
public class Sign {
protected static String SIGNS;
protected static final String SIGNS_FORMAT = "Welcome to the %s of the %s %s";
private static final String[] BUILDING_TYPES = new String[]{
"Inn",
"Inn",
"Tavern",
"Pub",
"Inn"
};
private static final String[] VERB_NAMES = new String[] {
"Dancing",
"Prancing",
"Eating",
"Jigging",
"Digging",
"Flogging",
"Floating",
"Flying",
"Laughing",
"Smiling",
"Drowning",
"Golden",
"Silver",
"Copper",
"Farming",
"Running",
"Sewing",
"Black",
"White",
"Fighting",
"Gesticulating"
};
private static final String[] NOUN_NAMES = new String[] {
"Pony",
"Horse",
"Griffin",
"Dragon",
"Wench",
"Bastard",
"Ogre",
"Troll",
"Ox",
"Cow",
"Cock",
"Hen",
"Ram",
"Ewe",
"Dog",
"Merchant",
"Fisherman",
"Arborist",
"Archer",
"Gallbladder",
"Pancreas"
};
private String currentSign = getNewSign();
public void reset(){
buildingText();
verbText();
nounText();
SIGNS = currentSign;
}
public void read(int pos) {
//referenced by pos in order to make the sign catch fire later on
GameScene.show(new WndMessage(SIGNS));
}
private String getNewSign(){
return String.format(SIGNS_FORMAT, buildingText(), verbText(), nounText());
}
private String buildingText() {
return BUILDING_TYPES[Random.Int(0, BUILDING_TYPES.length - 1)];
}
private String verbText() {
return VERB_NAMES[Random.Int(0, VERB_NAMES.length - 1)];
}
private String nounText() {
return NOUN_NAMES[Random.Int(0, NOUN_NAMES.length - 1)];
}
}
答案 0 :(得分:1)
当您使用静态最终字符串时,JVM会在启动期间创建这些字符串。 为了做你想做的事情,使用一个返回字符串实例的单例模式。然后reset()方法可以在需要时重置您正在使用的实例。
答案 1 :(得分:0)
感觉有点像你试图滥用静态变量,但你可以这样做。
public class Sign {
// These are no longer final as you need to change them
private static String TXT_PLACE_NAME;
private static String TXT_VERBING;
private static String TXT_NOUN;
protected static String SIGNS;
// Since they're not final reset can change them.
public void reset(){
TXT_PLACE_NAME = buildingText();
TXT_VERBING = verbText();
TXT_NOUN = nounText();
SIGNS = "Welcome to the "+ "\n \n "+ TXT_PLACE_NAME+ " of the "+ TXT_VERBING + TXT_NOUN + "";
}
// This ensures that they get initialized initially
static {
reset();
}
// ...
}
答案 2 :(得分:0)
从您的代码中,您使用的是实例方法reset()
和read()
,因此我假设您正在创建Sign
的不同实例。< / p>
那么为什么不让每个Sign对象保留自己的签名消息,并将代码重构为以下内容:
public class Sign {
protected static final String SIGNS_FORMAT = "Welcome to the %s of the %s %s";
private static final String[] BUILDING_TYPES = new String[]{
"Inn",
"Inn",
"Tavern",
"Pub",
"Inn"
};
private static final String[] VERB_NAMES = new String[] {
"Dancing",
"Prancing",
"Eating",
"Jigging",
"Digging",
"Flogging",
"Floating",
"Flying",
"Laughing",
"Smiling",
"Drowning",
"Golden",
"Silver",
"Copper",
"Farming",
"Running",
"Sewing",
"Black",
"White",
"Fighting",
"Gesticulating"
};
private static final String[] NOUN_NAMES = new String[] {
"Pony",
"Horse",
"Griffin",
"Dragon",
"Wench",
"Bastard",
"Ogre",
"Troll",
"Ox",
"Cow",
"Cock",
"Hen",
"Ram",
"Ewe",
"Dog",
"Merchant",
"Fisherman",
"Arborist",
"Archer",
"Gallbladder",
"Pancreas"
};
private String currentSign = getNewSign();
public void reset(){
currentSign = getNewSign();
}
public void read(int pos) {
GameScene.show(new WndMessage(currentSign));
// the `pos` argument is not used?
}
private String getNewSign(){
return String.format(SIGNS_FORMAT, buildingText(), verbText(), nounText());
}
private String buildingText() {
return BUILDING_TYPES[Random.Int(0, BUILDING_TYPES.length - 1)];
}
private String verbText() {
return VERB_NAMES[Random.Int(0, VERB_NAMES.length - 1)];
}
private String nounText() {
return NOUN_NAMES[Random.Int(0, NOUN_NAMES.length - 1)];
}
}
请注意,我不知道Random.Int
是什么,并假设它需要一个包含范围Random.Int(lowerBound, upperBound)
。
JDK方法应该是java.util.Random.nextInt(bound)
。