命令行参数 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21648427/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:51:35  来源:igfitidea点击:

Command line arguments Java

javacommand-linestatic

提问by user3277779

public class Test{
int Trails;
int Days
     public static void main (String []args){
     if(args.length!=0){
        numT = Integer.parseInt(args[0]);
        numD = Integer.parseInt(args[1]);
     }
     Trails=numT;
     Days=numD;
     }
}

I am trying to get input from the command line and then store them into globals but because they are being done in the main, it wants me to make everything static. Is there another way I should be doing this so that I can then do things with the data?

我试图从命令行获取输入,然后将它们存储到全局变量中,但因为它们是在 main 中完成的,所以它希望我将所有内容都设为静态。有没有另一种方法我应该这样做,以便我可以对数据进行处理?

采纳答案by initramfs

The main(String[] args)method gets invoked by the JVM "statically" and no actual object gets created. But there isn't any reason against creating an object of the enclosing class as such:

main(String[] args)方法由JVM“静态”调用,并且没有创建实际对象。但是没有任何理由反对像这样创建封闭类的对象:

public class Test{
    int Trails;
    int Days

    public static void main (String[] args){
        //Create object of type Test
        Test t = new Test();

        if(args.length!=0){
            t.Trails = Integer.parseInt(args[0]);
            t.Days = Integer.parseInt(args[1]);
        }
    }
}

Of course, you can also pass the parameters through a constructor as such:

当然,您也可以通过构造函数传递参数,如下所示:

public class Test{
    int Trails;
    int Days

    public Test(int numT, int numD){
        Trails = numT;
        Days = numD;
    }

    public static void main (String[] args){
        int numT;
        int numD;

        if(args.length!=0){
            numT = Integer.parseInt(args[0]);
            numD = Integer.parseInt(args[1]);

            //Create object here
            Test t = new Test(numT, numD);
        }
    }
}

回答by Kumar Abhinav

public class Test{
int Trails;
int Days
     public static void main (String []args){
     if(args.length!=0){

       Test test = new Test();
        test.Trails= Integer.parseInt(args[0]);
        test.Days= Integer.parseInt(args[1]);
     }
}

You need an object to work with instance variables

您需要一个对象来处理实例变量

回答by Martin Dinov

You need to pass them to an object instance that has a constructor or a method that accepts those command line args, then sets its instance variables (say, trailsand days) in the constructor or method. The only way to have instance variables is to have an instance of an object. Also, note that you shouldn't capitalize variable names.The convention is to use camelCase with the first letter being lowercase. Class names start with a capital letter.

您需要将它们传递给具有构造函数或接受这些命令行参数的方法的对象实例,然后在构造函数或方法中设置其实例变量(例如trailsdays)。拥有实例变量的唯一方法是拥有一个对象的实例另请注意,您不应将变量名称大写。约定是使用驼峰命名,首字母小写。类名以大写字母开头。

回答by Zied Hamdi

You're confusing the object notion with the static method concept:

您将对象概念与静态方法概念混淆了:

you can have many instances of objects like Test (test2, test2, etc...) each instance will have its own life: in real life the analogy is having many cars (instances) of the class (toyota corolla) for example: all cars are made as described by the model sepecification (the class)

您可以拥有多个对象实例,例如 Test(test2、test2 等...),每个实例都有自己的生命:在现实生活中,类比是类(toyota corolla)的许多汽车(实例),例如:所有汽车按照型号规格(类别)的描述制造

Then you have static methods which are methods that do not use a concrete instance: for example: security ckecks can be a static method: many cars come through a unique security check (that is not a function you can launch from within your car: it's not depending on the instance)

然后你有静态方法,它们是不使用具体实例的方法:例如:security ckecks 可以是一个静态方法:许多汽车通过独特的安全检查(这不是你可以从你的汽车内启动的功能:它是不取决于实例)

You can use this sample to understand better

您可以使用此示例更好地理解

public class Test{
  int trails;
  int days;

  public String toString() {
    return "trails :"+ trails +", days : "+ days;
  }
}

public class Launcher{
  public static void main (String []args){
     if(args.length!=0){
         Test test = new Test();
         test.trails= Integer.parseInt(args[0]);
         test.days= Integer.parseInt(args[1]);

         Test test2 = new Test();
         test2.trails= 5;
         test2.days= 2;

         Command cmd = new Command();
         cmd.doSomething(test);
         cmd.doSomething(test2);
         cmd.doSomething(test);
     }
  }
}

public class Command {
  Test lastTested;
  public void doSomething(Test data) {
    lastTested = data;
    System.out.println( "working with "+ lastTested );
  }
}

In the example above, you create an instance of Test that you fill with data. Then you create an instance of Command, that will save in its state the last executed Test instance, then print the test instance data through its toString() method.

在上面的示例中,您创建了一个用数据填充的 Test 实例。然后创建一个 Command 实例,它将在其状态中保存上次执行的 Test 实例,然后通过其 toString() 方法打印测试实例数据。

You can create a second instance of Test with other data and pass it to the same cmd (Command instance's method doSomething()). You will then see that the instance that is inside cmd changed, but getting back to your first instance test kept the values as expected

您可以使用其他数据创建第二个 Test 实例并将其传递给同一个 cmd(命令实例的方法 doSomething())。然后,您将看到 cmd 中的实例发生了变化,但返回到您的第一个实例测试时,这些值仍按预期保留