在其他类中获取变量 [英] Get variable in other classes

查看:111
本文介绍了在其他类中获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在其他类中获取变量 inString 。我如何做到这一点?

  public class main {
public static StringBuffer inString;


public static void main(String [] args){
inString = new StringBuffer(我们的目标是做一个15逼真的游戏,其中磨碎电力线和做尾巴是不容易的,就像在现实世界中一个游戏,你会觉得你是真正的骑术。一个游戏,其中创造力和美容相结合。
inString = new StringBuffer(inString.toString()。replaceAll(+,));
}
}

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



您将获得null,因为inString从未初始化为Robert Kilar正确指向的注释。您可以使用类名称来引用静态变量。



示例ClassName.variablename。在您的情况下

  main.inString 

运行你的主类。当你运行inString时在类的构造函数中初始化。现在你可以参考下面的Myclass相同。

  public class main {
public static StringBuffer inString;

public main()
{
inString = new StringBuffer(我们的目标是做一个15逼真的游戏,其中磨削电力线和做尾巴是不容易的,在现实世界中,一个游戏,你会觉得你是一个真正的骑术,一个游戏,其中创造力和美容相结合。
inString = new StringBuffer(inString.toString()。replaceAll(+,));
new MyClass();
}
public static void main(String [] args){
new main();
}
}



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

  class MyClass {

public MyClass(){
System.out.println(。 ............+ main.inString); //引用静态变量
}
}

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

  public class main {
public StringBuffer inString;

public main()
{
inString = new StringBuffer(我们的目标是做一个15逼真的游戏,其中磨削电力线和做尾巴是不容易的,在现实世界中,一个游戏,你会觉得你是一个真正的骑术,一个游戏,其中创造力和美容相结合。
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);
}
}

请检查链接@ 为什么静态变量被认为是邪恶的?


I need to get variable inString in other class. How can I do this?

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(" +", " "));
        }
}

So I try to use System.out.println(main.inString); in my Textcl.class, but get null.

解决方案

You will get null because inString is never initialized as rightly pointed by Robert Kilar in the comment.

You refer to static variables by using the class name.

Example ClassName.variablename. In your case

    main.inString 

Run your main class. When you run inString is initialized in the constructor of the class. Now you can refer to the same in Myclass as below.

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();
    }
}

Now in MyClass refer to the static variable.

class MyClass {

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

You can also pass variables to the constructor of a class.

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();

    }
}

Then in Myclass

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

Please check the link @ Why are static variables considered evil?

这篇关于在其他类中获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆