如何从 Java 中的另一个类获取一些变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20864211/
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 do I get some variable from another class in Java?
提问by user3137831
I am "playing around" with Java, watching tutorials and trying to get the hang of it. For this question, I'm trying to figure out how I can take a variable from another class and use it in my main one, without making the initial variable public. Here is the code:
我正在“玩转”Java,观看教程并试图掌握它。对于这个问题,我试图弄清楚如何从另一个类中获取一个变量并在我的主要类中使用它,而无需公开初始变量。这是代码:
I am trying to get int x
equal to 5 (as seen in the setNum()
method), but when it prints it gives me 0.
我试图int x
等于 5(如setNum()
方法中所示),但是当它打印时它给了我 0。
Main Class:
主要类:
package getVarTest;
public class Main {
public static void main (String[]args){
Vars varsObject = new Vars();
int x = varsObject.getNum();
System.out.println(x);
}
}
Variable Class:
变量类:
package getVarTest;
public class Vars {
private int num;
public void setNum(int x){
this.num = 5;
}
public int getNum(){
return num;
}
}
So, as you can see I am trying to take the private int num
and make the int x
in the main class equal to it.
所以,正如你所看到的,我试图private int num
让int x
主类中的 与它相等。
采纳答案by Elliott Frisch
Do NOTdo that! setNum(num);//fix- until someone fixes your setter. Your getter should not call your setter with the uninitialized value of
num(e.g.
0`).
千万不要做!setNum(num);//fix- until someone fixes your setter. Your getter should not call your setter with the uninitialized value of
编号(e.g.
0`)。
I suggest making a few small changes -
我建议做一些小的改变——
public static class Vars {
private int num = 5; // Default to 5.
public void setNum(int x) {
this.num = x; // actually "set" the value.
}
public int getNum() {
return num;
}
}
回答by Peter Lawrey
I am trying to get int x equal to 5 (as seen in the setNum() method) but when it prints it gives me 0.
我试图让 int x 等于 5(如 setNum() 方法中所示)但是当它打印时它给了我 0。
To run the code in setNum
you have to call it. If you don't call it, the default value is 0
.
要在其中运行代码,setNum
您必须调用它。如果不调用,默认值为0
。
回答by MillaresRoo
You never call varsObject.setNum();
你从不打电话 varsObject.setNum();
回答by Daniel Nuriyev
Your example is perfect: the field is private and it has a getter. This is the normal way to access a field. If you need a direct access to an object field, use reflection. Using reflection to get a field's value is a hack and should be used in extreme cases such as using a library whose code you cannot change.
你的例子是完美的:该字段是私有的,它有一个吸气剂。这是访问字段的正常方式。如果您需要直接访问对象字段,请使用反射。使用反射来获取字段的值是一种技巧,应该在极端情况下使用,例如使用其代码无法更改的库。
回答by Zinov
The code that you have is correct. To get a variable from another class you need to create an instance of the class if the variable is not static, and just call the explicit method to get access to that variable. If you put get and set method like the above is the same of declaring that variable public.
您拥有的代码是正确的。要从另一个类获取变量,如果变量不是静态的,您需要创建该类的一个实例,并且只需调用显式方法即可访问该变量。如果像上面那样放置 get 和 set 方法,则与将该变量声明为 public 相同。
Put the method setNum private and inside the getNum assign the value that you want, you will have "get" access to the variable in that case
将方法 setNum 设为私有并在 getNum 中分配您想要的值,在这种情况下您将可以“获取”访问该变量
回答by peachoftree
If the variable is public you can get it just by saying packageName.ClassName.variableName
, but if it is private you will have to make a getter method inside the class that the variable is in. It will look something like this:
如果变量是公共的,你可以通过说 得到它packageName.ClassName.variableName
,但如果它是私有的,你将不得不在变量所在的类中创建一个 getter 方法。它看起来像这样:
public int getVariableName() {
return variableName;
}
Then just call that method wherever you need it.
然后只需在需要的地方调用该方法即可。