如何在另一个类中使用静态类中的变量?
I's trying to build an encoder like the "Enigma Machine" and I have this code so far where I'm trying to get comboOne() to use the "s" variable from main():
我正在尝试构建像“Enigma Machine”这样的编码器,到目前为止我有这个代码,我试图让comboOne()使用main()中的“s”变量:
import java.io.*;
public class main
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What would you like to encode?");
String s = br.readLine();
s.toCharArray();
System.out.println(s);
//comboOne comboOne = new comboOne();
}
}
Just to encode one letter I've written this:
只是编码一个字母我写了这个:
public class comboOne extends main
{
main m = new main();
char message = s.toCharArray();
if(message == 'a')
{
System.out.println('b');
}
}
I am quite new so apologies if I am making an obvious mistake but I think this would be a fun challenge for myself. Please send help and thanks for helping :)
如果我犯了一个明显的错误,我很新,所以道歉,但我认为这对我自己来说是一个有趣的挑战。请发送帮助并感谢您的帮助:)
1 个解决方案
#1
-1
Add outside your Main.main method
在Main.main方法之外添加
public static String s;
Then use Main.s
to access the String in a static way. Since it is static it does not need to be instantiated an can directly be called from the class.
然后使用Main.s以静态方式访问String。由于它是静态的,因此不需要实例化,可以直接从类中调用。