【问题标题】:Get variable in other classes获取其他类中的变量
【发布时间】:2013-04-06 16:01:11
【问题描述】:

我需要在其他类中获取变量inString。我该怎么做?

public class main {
    public static StringBuffer inString;


    public static void main(String[] args) {
        inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. ");
        inString = new StringBuffer(inString.toString().replaceAll(" +", " "));
        }
}

所以我尝试在我的 Textcl.class 中使用 System.out.println(main.inString);,但得到 null

【问题讨论】:

  • 因为你从来没有在你运行的其他静态 void main 中初始化它

标签: java class


【解决方案1】:

您可以通过以下方式访问它:main.inString 其中main 是声明public static 变量的类的名称。

【讨论】:

  • 不要忘记main.inString中的大写S
  • 试试这个,但无法获取变量(
【解决方案2】:

您将得到 null,因为 inString 从未像 Robert Kilar 在评论中正确指出的那样初始化。

您使用类名来引用静态变量。

示例 ClassName.variablename。你的情况

    main.inString 

运行你的主类。当你运行 inString 时,在类的构造函数中被初始化。现在您可以在 Myclass 中引用相同的内容,如下所示。

public class main {
public static StringBuffer inString;

public main()
{
inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. ");
inString = new StringBuffer(inString.toString().replaceAll(" +", " "));
new MyClass();
}
public static void main(String[] args) {
    new main();
    }
}

现在在 MyClass 中引用静态变量。

class MyClass {

public MyClass() {
    System.out.println("............."+main.inString);// refer to static variable
}
}

您还可以将变量传递给类的构造函数。

public class main {
public  StringBuffer inString;

 public main()
  {
    inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. ");
    inString = new StringBuffer(inString.toString().replaceAll(" +", " "));  
    new MyClass(inString);
 }
public static void main(String[] args) {
    new main();

    }
}

然后在 Myclass 中

 public class MyClass
 {
        public MyClass(StringBuffer value)
        {
          System.out.println("............."+value);
        }
 } 

请查看链接@Why are static variables considered evil?

【讨论】:

    【解决方案3】:

    由于您将类中的字段设为静态,因此您可以使用类名来访问它,即

    main.inString
    

    【讨论】:

      【解决方案4】:

      使用JavaBeans 并将其作为其字段之一存储在其中,并为此使用getter 和setter。

      JavaBean 是具有属性的 Java 类。将属性视为私有实例变量。由于它们是私有的,因此从类外部访问它们的唯一方法是通过类中的方法。这 更改属性值的方法称为 setter 方法,检索属性值的方法称为 getter 方法。

      public class VariableStorage implements Serializable  {
      
          private String inString;
      
          public String getInString() {
              return inString;
          }
      
          public void setInString(String inString) {
              this.inString = inString;
          }
      }
      

      在你的邮件方法中设置变量:

      VariableStorage variableStorage = new VariableStorage();
      variableStorage.setInString(inString);
      

      然后使用对象序列化来序列化这个对象,并在你的其他类中反序列化这个对象。

      在序列化中,对象可以表示为一个字节序列,其中包括对象的数据以及有关对象类型和对象中存储的数据类型的信息。

      序列化对象写入文件后,可以从文件中读取并反序列化。即表示对象及其数据的类型信息和字节可用于在内存中重新创建对象。

      如果您想要这方面的教程,请参考 Serialization in Java

      【讨论】:

        猜你喜欢
        • 2021-06-21
        • 2023-01-18
        • 2016-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多