java 如何从另一个 void 方法访问 void 方法中的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17531088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access a string in a void method from another void method?
提问by joe dacoolguy
public void WeatherInfo(){
.......
String weatherLocation = weatherLoc[1].toString();
........
}
basically I have this dynamic string that is in a void called WeatherInfo.
基本上我有这个动态字符串,它在一个叫做 WeatherInfo 的空间中。
But I need to get the weatherLocation string from another void, like this
但我需要从另一个空白处获取 weatherLocation 字符串,就像这样
public void WeatherChecker(){
YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();
yahooWeatherUtils.queryYahooWeather(getApplicationContext(), weatherLocation, this);
}
Therefore I need to be able to access weatherLocation from this void.
因此,我需要能够从这个空隙访问 weatherLocation。
How do I do this?
我该怎么做呢?
回答by christopher
This is a question of scope. You have declared a local variable, so it can only be accessed locally. If you wish to access the variable outside of the method, either pass a reference or declare it globally.
这是一个范围问题。你已经声明了一个局部变量,所以它只能在本地访问。如果您希望在方法之外访问变量,请传递引用或全局声明它。
public void method1()
{
String str = "Hello";
// str is only accessible inside method1
}
String str2 = "hello there";
// str2 is accessible anywhere in the class.
Edit
编辑
As pointed out, you should look at the Java naming Conventions.
正如所指出的,您应该查看Java 命名约定。
回答by stinepike
You can do any of the following
您可以执行以下任何操作
1) pass the string as parameter and set the value
1) 将字符串作为参数传递并设置值
or
或者
2) use a member variable and use a getter to get the variable
2)使用成员变量并使用getter获取变量
回答by Dummy Code
You need to pass it as a parameter or create a "global variable"
您需要将其作为参数传递或创建一个“全局变量”
IE you could do either of the following...
IE 您可以执行以下任一操作...
public void methodOne(String string){
System.out.println(string);
}
public void methodTwo(){
String string = "This string will be printed by methodOne";
methodOne(string);
}
OR (a better solution)
或(更好的解决方案)
Create a global variable under the class declaration...
在类声明下创建一个全局变量...
public class ThisIsAClass {
String string; //accessible globally
....
//way down the class
public void methodOne(){
System.out.println(string);
}
public void methodTwo(){
String string = "This string will be printed by methodOne"; //edit the string here
methodOne();
}
Let me know if you have any questions. Of course you will have to change String string;
accordingly but it is the same concept with any variable.
如果您有任何问题,请告诉我。当然,您必须相应地更改String string;
,但对于任何变量都是相同的概念。
You said that your "sentence" is created in one of these methods and it hasn't been created when you declare it globally. All you need to do is create it globally, String weatherLocation = null;
and then set it whenever you need to. I think it your example it would be under weatherInfo()
你说你的“句子”是在这些方法之一中创建的,当你全局声明它时它没有被创建。您需要做的就是全局创建它,String weatherLocation = null;
然后在需要时进行设置。我认为这是你的例子,它将在weatherInfo()
public void WeatherInfo(){
weatherLocation = weatherLoc[1].toString();
}
Instead of creating a new one we just edit the one we created globally.
我们只是编辑我们在全局创建的一个,而不是创建一个新的。
-Henry
-亨利
回答by dghalbr
Maybe try declaring your String higher up in the scope of the method your attempting to use it. You would have to make sure WeatherInfo() was called first (maybe in a constructor) so that it gets initialized or you would get odd results.
也许尝试在您尝试使用它的方法的范围内声明您的 String 更高。您必须确保首先调用 WeatherInfo()(可能在构造函数中),以便对其进行初始化,否则您会得到奇怪的结果。
public Class {
String weatherLocation = null;
public void WeatherInfo(){
........
weatherLocation = weatherLoc[1].toString();
........
}
public void WeatherChecker(){
YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();
yahooWeatherUtils.queryYahooWeather(getApplicationContext(), weatherLocation, this);
}
}
回答by André Stannek
You can't do this directly as local variables in a method only exist from their declaration to the end of the block (which is the end of the method at latest).
您不能直接执行此操作,因为方法中的局部变量仅存在于从它们的声明到块的末尾(最迟是方法的末尾)。
If you need to access a variable in more than one method you can make it a class variable (or field). Once you assign a value to a field it is saved as state of the object and can be accessed and modified at any time later. This of course means you have to set it first.
如果您需要在多个方法中访问一个变量,您可以将其设为类变量(或字段)。为字段分配值后,它会保存为对象的状态,以后可以随时访问和修改。这当然意味着您必须先设置它。
回答by gwin003
I'm not 100% sure what you are trying to accomplish, or what functions are calling what other functions... but you could just pass weatherLocation
in as as a parameter. Is this what you are looking for?
我不是 100% 确定您要完成什么,或者哪些函数正在调用其他哪些函数……但您可以将weatherLocation
其作为参数传入。这是你想要的?
public void WeatherChecker(String weatherLocation){
YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();
yahooWeatherUtils.queryYahooWeather(getApplicationContext(), weatherLocation, this);
}