プログラムの実行中に文字列を文字列に置き換える方法がわかりません。これは可能ですか?プログラムの実行中に単語を別の単語に置き換えて、その行から実行を続けたいと考えています。
どうすればこれを行うことができますか? 皆さんありがとう
hstring.replace("picstart", "up1");
g.drawPixmap(Assets.picstart , 128, 160);
コードは次のようになります。
if (condition) {
g.drawPixmap(Assets.picstart , 128, 160);
} else {
g.drawPixmap(Assets.up1 , 128, 160);
}
リフレクションは使用しないでください。アセットコードが画像やテクスチャなどを処理すると仮定すると、次のような場合があります。
String hstring = "picstart";
// ... stuff happens
// that forces us to change hstring! ...
hstring = hstring.replace("up1"); // or you could just say hstring = "up1";
g.drawPixmap(Assets.getAssetFor(hstring), 128, 160);
次に、静的Assetクラスで次のことができます。
public PixelMap getAssetFor(String identifier) {
if (identifier.equals("picstart") {
return new PicStartPixelMap();
}
else if (identifier.equals("up1")) {
return new UpOnePixelMap();
}
}
String
つまり、結果を変数に代入する必要があります。
hstring = hstring.replace(...);