如何在另一种方法中获取变量值 - 对如何进行感到困惑
How to get variables value in another method - confused about how to proceed
我是 Java 的新手(也是我在 Whosebug 上提出的第一个问题),现在我正在为如何将一个方法中定义的一些变量值传递给另一个方法而苦恼。
我已经对全局变量、ArrayList、HashMap 等内容进行了多次搜索,但唯一似乎是我正在搜索的内容 (
Global variables in Java) 让我对如何进行更加困惑。
我已经尝试为此使用 ArrayList,但它没有用 - 而且我不知道我是否可以将它用于我想做的事情...
这是我的代码:
public static void creationGuilde(String[] args, Player player, String playerName)
{
String nomGuilde = args[2];
String message1 = "Votre nouvelle guilde se nommera " + nomGuilde + ".";
TextComponent confirmer = new TextComponent("Cliquez ici pour confirmer");
TextComponent annuler = new TextComponent("cliquez ici pour annuler");
String message2 = confirmer + "OU" + annuler + ".";
player.sendMessage(message1);
player.sendMessage(message2);
confirmer.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeConfirmer"));
annuler.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeAnnuler"));
}
private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().equals("creationGuildeConfirmer"))
{
String guilde = CommandeGuilde.creationGuilde(nomGuilde);
event.getPlayer().sendMessage("Félicitations! Vous venez de créer la guilde " +guilde); // <-- Here, trying to get the value of 'guilde' in the 'creationGuilde' method...
}
}
我想做的是从"onPreCreationGuildeCommand"方法,我想从"creationGuilde"方法得到'nomGuilde'值放在我最后一个sendMessage上。
我希望我的问题足够清楚。感谢您在这方面帮助我。
您可以在方法之外定义变量 nonGuilde,global variable.When 您可以定义一个全局变量,您可以 "write" 或者从这个 class 的所有方法中获取这个变量。
第二种解决方案是从 creationGuilde 方法 return nonGuilde。
最简单也可能是最好的解决方案是将 nonGuilde 定义为全局变量。
代码应如下所示:
class YourClassName{
//Must be null
//otherwise if you call onPreCreationGuildeCommand before creationGuilde
//you would get error because variable hasn't been initialized
private String nomGuilde = null;
public static void creationGuilde(String[] args, Player player, String playerName){
//set the value
//value will persist outside the method because variable is global
nomGuilde = args[2];
}
private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event){
// Here you can do anything with variable, for example:
System.out.println(nomGuilde);
}
}
另外,我的建议是阅读一些关于变量及其范围(特定变量可见的代码块)和它们的生命周期(变量在内存中存在多长时间)
我是 Java 的新手(也是我在 Whosebug 上提出的第一个问题),现在我正在为如何将一个方法中定义的一些变量值传递给另一个方法而苦恼。
我已经对全局变量、ArrayList、HashMap 等内容进行了多次搜索,但唯一似乎是我正在搜索的内容 ( Global variables in Java) 让我对如何进行更加困惑。
我已经尝试为此使用 ArrayList,但它没有用 - 而且我不知道我是否可以将它用于我想做的事情...
这是我的代码:
public static void creationGuilde(String[] args, Player player, String playerName)
{
String nomGuilde = args[2];
String message1 = "Votre nouvelle guilde se nommera " + nomGuilde + ".";
TextComponent confirmer = new TextComponent("Cliquez ici pour confirmer");
TextComponent annuler = new TextComponent("cliquez ici pour annuler");
String message2 = confirmer + "OU" + annuler + ".";
player.sendMessage(message1);
player.sendMessage(message2);
confirmer.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeConfirmer"));
annuler.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeAnnuler"));
}
private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().equals("creationGuildeConfirmer"))
{
String guilde = CommandeGuilde.creationGuilde(nomGuilde);
event.getPlayer().sendMessage("Félicitations! Vous venez de créer la guilde " +guilde); // <-- Here, trying to get the value of 'guilde' in the 'creationGuilde' method...
}
}
我想做的是从"onPreCreationGuildeCommand"方法,我想从"creationGuilde"方法得到'nomGuilde'值放在我最后一个sendMessage上。
我希望我的问题足够清楚。感谢您在这方面帮助我。
您可以在方法之外定义变量 nonGuilde,global variable.When 您可以定义一个全局变量,您可以 "write" 或者从这个 class 的所有方法中获取这个变量。 第二种解决方案是从 creationGuilde 方法 return nonGuilde。
最简单也可能是最好的解决方案是将 nonGuilde 定义为全局变量。
代码应如下所示:
class YourClassName{
//Must be null
//otherwise if you call onPreCreationGuildeCommand before creationGuilde
//you would get error because variable hasn't been initialized
private String nomGuilde = null;
public static void creationGuilde(String[] args, Player player, String playerName){
//set the value
//value will persist outside the method because variable is global
nomGuilde = args[2];
}
private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event){
// Here you can do anything with variable, for example:
System.out.println(nomGuilde);
}
}
另外,我的建议是阅读一些关于变量及其范围(特定变量可见的代码块)和它们的生命周期(变量在内存中存在多长时间)