Java:如何在一个类中拥有全局值? [英] Java: how to have global values inside a class?

查看:204
本文介绍了Java:如何在一个类中拥有全局值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要更少的方法。我想要一个通用的全局TestClass,我可以在其中使用它的任何值。

  import java.util。*; 
import java.io. *;

public class TestClass {
TestClass(String hello){
String hallo = hello;
String halloSecond =Saluto!;

public static void main(String [] args){
TestClass test = new TestClass(Tjena!);
System.out.println(我想要Tjena!:+ test.hallo);
TestClass testSecond = new TestClass(1);
System.out.println(我想要Saluto !:+ test.halloSecond);
System.out.println(我想要Saluto !:+ testSecond.halloSecond);

//如何获得像Saluto!这样的glob.vars?


code

$ b

[需要说明] 我无法理解GLOB.VARS的不使用。请查看下面的代码,在没有实例的情况下无法访问GLOB.VARS,因此出现错误。如果我保证没有恶意代码可以创建实例,那么在使用GLOB.vars时是否有任何问题?

  $ javac TestClass.java 
TestClass.java:19:非静态变量hallo不能从静态上下文中引用
System.out.println(它没有进入GLOB.VAR:+ hallo);
^
1错误
$ cat TestClass.java
import java.util。*;
import java.io. *;

public class TestClass {
public String hallo;
public String halloSecond;

TestClass(String hello){
hallo = hello;
halloSecond =Saluto!;

public static void main(String [] args){
TestClass test = new TestClass(Tjena!);
System.out.println(没有得到GLOB.VAR+ hallo);
}
}


解决方案

I猜测你正在寻找这样的东西:

  public class TestClass {
public final String hallo;
public static final String halloSecond =Saluto!;

TestClass(String hello){
String hallo = hello;


public static void main(String [] args){
TestClass test = new TestClass(Tjena!);
System.out.println(我想要Tjena!:+ test.hallo);
TestClass testSecond = new TestClass(1);
System.out.println(我想要Saluto !:+ test.halloSecond);
System.out.println(我想要Saluto !:+ testSecond.halloSecond);


hallo 设置在 TestClass 的每个实例中。 halloSecond 的值是一个常量,由类的所有实例共享,并且整个应用程序都可见。请注意,使用此代码,IDE /编译器可能会在 test.halloSecond 上给出警告 - 它应该由类名限定,如 TestClass。 halloSecond ,而不是实例名称。



全局变量更新:全局变量的主要问题是他们让代码难以理解 - 如果一个方法使用全局变量,那么您就看不到简单的代码。 从它的签名中得到它实际操作的数据

  • 难以测试 - 同样的方法在单元测试中很难独立测试,因为您必须(重新)设置所有全局变量都依赖于每个单元测试之前所需的状态

  • 难以维护 - 全局变量创建依赖关系,这很容易使代码陷入混乱的混乱状态所有东西都依赖于其他任何东西



  • 在Java中,一切都在一个类中,所以你可以没有像C / C ++这样的经典全局变量。但是,公共静态数据成员仍然是一个全局变量。



    请注意,上面的代码示例 halloSecond 是一个全局常量,而不是一个变量(因为它被声明为 final 并且是不可变的),这可以减轻很多这些问题依赖性问题)。


    I want less methods. I want a common global TestClass from which I could use any of its value inside the class.

    import java.util.*;
    import java.io.*;
    
    public class TestClass {
            TestClass(String hello){
                    String hallo = hello;
                    String halloSecond = "Saluto!";
            }
            public static void main(String[] args) {
                    TestClass test = new TestClass("Tjena!");
                    System.out.println("I want "Tjena!": " + test.hallo);
                    TestClass testSecond = new TestClass("1");
                    System.out.println("I want Saluto!:" + test.halloSecond);
                    System.out.println("I want Saluto!:" + testSecond.halloSecond);
    
                    // How can I get glob.vars like the "Saluto!"?
            }
    }
    

    [Clarification Needed] I cannot understand the no-use of GLOB.VARS. Please, see the code belowe where you cannot access the GLOB.VARS without an instance, hence the error. If I quarantee no malicious code can make an instance, is there any problem in using GLOB.vars?

    $ javac TestClass.java 
    TestClass.java:19: non-static variable hallo cannot be referenced from a static context
      System.out.println("It did not get to the GLOB.VAR: " + hallo);
                                                 ^
    1 error
    $ cat TestClass.java 
    import java.util.*;
    import java.io.*;
    
    public class TestClass {
            public String hallo;
            public String halloSecond;
    
            TestClass(String hello){
                    hallo = hello;
                    halloSecond = "Saluto!";
            }
            public static void main(String[] args) {
                    TestClass test = new TestClass("Tjena!");
          System.out.println("It did not get to the GLOB.VAR" + hallo);
            }
    }
    

    解决方案

    I guess you are looking for something like this:

    public class TestClass {
        public final String hallo;
        public static final String halloSecond = "Saluto!";
    
        TestClass(String hello){
            String hallo = hello;
        }
    
        public static void main(String[] args) {
            TestClass test = new TestClass("Tjena!");
            System.out.println("I want "Tjena!": " + test.hallo);
            TestClass testSecond = new TestClass("1");
            System.out.println("I want Saluto!:" + test.halloSecond);
            System.out.println("I want Saluto!:" + testSecond.halloSecond);
        }
    }
    

    The value of hallo is set in each instance of TestClass. The value of halloSecond is a constant, shared by all instances of the class and visible for the whole app. Note that with this code your IDE/compiler probably gives you a warning upon test.halloSecond - it should be qualified by the class name, like TestClass.halloSecond, rather than an instance name.

    Update on global variables: the main problem with global variables is that they make the code

    • harder to understand - if a method uses global variables, you can't see simply from its signature what data is it actually manipulating
    • harder to test - same method is difficult to test isolated in unit tests, as you have to (re)set all global variables it depends on to the desired state before each unit test
    • harder to maintain - global variables create dependencies, which easily make the code into a tangled mess where everything depends on everything else

    In Java everything is inside a class, so you can't have "classic" global variables like in C/C++. However, a public static data member is still in fact a global variable.

    Note that the code sample above, halloSecond is a global constant, not a variable (as it is declared final and is immutable), which alleviates much of these problems (except maybe the dependency issue).

    这篇关于Java:如何在一个类中拥有全局值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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