Java (eclipse):如何从同一项目中的另一个类调用 int 或 string?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25428768/
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
Java (eclipse): How to call a int or string from another class in same project?
提问by Cristobal Ruiz
How can I use an int or string from another class in same project, for example, here we go with a class called Estadisticas:
如何在同一个项目中使用另一个类中的 int 或 string,例如,这里我们使用一个名为 的类Estadisticas:
public class Estadisticas {
public void stats() {
final String c1 = "Tu personaje";
final int Nivel = 1;
final int Salud = 50;
final int Ataque = 2;
final int Defensa = 1;
final String c2 = "Primer monstruo - Rata rabiosa";
final int SaludM1 = 2;
final int AtaqueM1 = 1;
}
}
And I want to know how to call for example the String c1 to println like this:
我想知道如何像这样调用 String c1 到 println:
public class Start extends Estadisticas {
public static void main() {
System.out.println(String c1);
}
}
After reading some example codes and people with similar problem, I tried with extends Estadisticaswith no luck, and I'm putting this code as I don't know how to call that string from another class in same project.
在阅读了一些示例代码和有类似问题的人之后,我尝试了但extends Estadisticas没有运气,我把这段代码放在了,因为我不知道如何从同一个项目中的另一个类调用该字符串。
Thanks in advance! Cristobal.
提前致谢!克里斯托瓦尔。
回答by ElRoBe
If you're trying to call a variable from another class, you have several options:
如果您尝试从另一个类调用变量,则有多种选择:
1) Declare the variable in the other class as static:
1) 将另一个类中的变量声明为静态:
public class Estadisticas {
public static String c1 = "Tu personaje";
...
}
So in the other class, you can just call the string like so:
所以在另一个类中,你可以像这样调用字符串:
public class Start extends Estadisticas {
public static void main() {
System.out.println(Estadisticas.c1);
}
}
2) Have the stats method return the string:
2) 让 stats 方法返回字符串:
public String stats() {
final String c1 = "Tu personaje";
final int Nivel = 1;
final int Salud = 50;
final int Ataque = 2;
final int Defensa = 1;
final String c2 = "Primer monstruo - Rata rabiosa";
final int SaludM1 = 2;
final int AtaqueM1 = 1;
return c1;
}
So you can do this:
所以你可以这样做:
public class Start extends Estadisticas {
public static void main() {
Estadisticas es = new Estadisticas();
System.out.println(es.stats());
}
}
3) Use an accessor method and instance variables:
3)使用访问器方法和实例变量:
public class Estadisticas {
private String c1, c2;
private int nivel, salud, ataque, defensa, saludM1, ataqueM1;
public void stats() {
c1 = "Tu personaje";
nivel = 1;
salud = 50;
ataque = 2;
defensa = 1;
c2 = "Primer monstruo - Rata rabiosa";
saludM1 = 2;
ataqueM1 = 1;
}
//accessor method
public String getc1() {
return c1;
}
}
So in the other class you can use:
所以在另一个类中你可以使用:
public class Start extends Estadisticas {
public static void main() {
Estadisticas es = new Estadisticas();
System.out.println(es.getc1());
}
}
回答by TheLostMind
Like this :
像这样 :
public class Estadisticas {
final String c1 = "Tu personaje";
final int Nivel = 1;
final int Salud = 50;
final int Ataque = 2;
final int Defensa = 1;
final String c2 = "Primer monstruo - Rata rabiosa";
final int SaludM1 = 2;
final int AtaqueM1 = 1;
public void stats() {
}
}
public class Start extends Estadisticas {
public static void main() {
Estadisticas es = new Estadisticas (); // create instance here
System.out.println(es.c1); // print using the instance
}
}
回答by hurricane
this is public example. In java you need to use them as private for security.
这是公开的例子。在 java 中,您需要将它们用作私有以确保安全。
public class Estadisticas {
public String c1 = "Tu personaje";
public int Nivel = 1;
}
This is calling from another class.
这是来自另一个类的调用。
public class Start {
public static void main() {
Estadisticas.Nivel;
Estadisticas.c1 ;
}
}

