Java的传递命令行参数的方法 [英] Java passing command line arguments to methods

查看:213
本文介绍了Java的传递命令行参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序,它有两个词作为命令行参数,做一些事情给他们,并打印出结果。我正在写一个类来处理这个问题,我的问题是:什么是通过给出的一类方法之间的命令行参数两个词的最好方法?为什么我不能用通常的this.variable =与ARGS构造?

I'm writing a program, which takes two words as command line arguments, does something to them, and prints out the result. I'm writing a class to handle this, and my question is: what is the best way to pass two words given as command line arguments between methods in a class? Why can't I use the usual "this.variable = " in the constructor with "args"?

推荐答案

可以,如果你通过 ARGS 来构造:

You can, if you pass args to the constructor:

public class Program
{
    private String foo;
    private String bar;

    public static void main(String[] args) 
    {
        Program program = new Program(args);
        program.run();
    }

    private Program(String[] args)
    {
        this.foo = args[0];
        this.bar = args[1];
        // etc
    }

    private void run()
    {
        // whatever
    }
}

这篇关于Java的传递命令行参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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