传递命令行参数给不同的类

huangapple go评论69阅读模式
英文:

Passing command line arguments to different class

问题

我正在为一个包含两个类的Java应用程序提供命令行参数。在包含main()方法的类中使用的参数工作正常,但其他参数给我带来了"找不到符号"的错误。我认为我只是在错误的地方提供了args,但似乎无法得到正确的位置。

以下是引发问题的类中的代码片段:

public abstract class Sample {

    public Sample() throws Exception {
        initialise(args);
    }

    public void initialise() {
        //做一些事情
    }
}

而我正在尝试在这个方法中使用args:

private SampleIF getPrincipal() {
    SampleIF xyz = auth(args[0], args[1]);
}

auth() 方法类似于以下内容:

protected SampleIF auth(String user, String pass) {
    //做一些事情
}

希望这有助于解决你的问题。

英文:

I'm providing command line arguments to a java application with two classes. The arguments used by methods in the class containing main() work fine, but the other arguments give me 'can't find symbol' errors. I think I am just providing args in the wrong place but I can't seem to get the right one.

Here is a snippet of the code in the class causing the issue:

public abstract class Sample { 

public Sample() throws Exception {
        initialise(args);
}

public void initialise() {
        //do something
}
}

and i'm trying use the args in this method:

private SampleIF getPrincipal() {
        SampleIF xyz = auth(args[0], args[1]);
}

The auth() method looks similar to this:

protected SampleIF auth(String user, String pass) {
        //do something
}

答案1

得分: 2

I see nowhere in your Sample class where it's accepting args to pass around. It should be like

public class Sample {
    public Sample(String[] args) {
        initialize(args);
    }
    public void initialize(String[] args) {
        // Do something
    }
}
public class Main {
    public static void main(String[] args) {
        Sample s = new Sample(args);
    }
}
英文:

I see nowhere in your Sample class where it's accepting args to pass around. It should be like

public class Sample {
    public Sample(String[] args) {
        initialize(args);
    }
    public void initialize(String[] args) {
        // Do something
    }
}
public class Main {
    public static void main(String[] args) {
        Sample s = new Sample(args);
    }
}

huangapple
  • 本文由 发表于 2020年8月11日 21:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63358759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定