英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论