这可能看起来很简单(甚至很愚蠢),但我想不出一个好方法来搜索我想要的东西,甚至标题也可能不完全正确。
我创建了一个标准的独立 java 程序,其中包含一个 main() 函数,它可以完成我想要的操作。我意识到我可能需要使用将来可能需要的其他程序来调用该程序。所以我决定创建一个构造函数,它将使我能够从任何 java 类进行调用。
我的问题是这种方法是否正确
public class GetUploadFiles {
private static String[] arguments;
/**
* Constructor
* expected a String[] containing the information needed
* @param args the arguments passed
*/
public GetUploadFiles(String[] args){
arguments=args;
}
/**
* start the process of creating and uploading the zip file according to the arguments passed to the constructor
* @throws IOException
*/
public static void upload() throws IOException{
startProcess(arguments);
System.exit(0);
}
public static void main(String[] args) throws IOException {
startProcess(args);
System.exit(0);
}
/**
* start the process
* @param args
* @throws IOException
*/
public static void startProcess(String[] args) throws IOException{
//my code is here
}
}//class
谢谢
最佳答案
我认为这是一个可以接受的解决方案。需要注意的一件事是,您的上传方法不应该是静态的。我个人会推荐这样的东西:
public class GetUploadFiles {
private static String[] arguments;
public GetUploadFiles(String[] args){
arguments=args;
}
/**
* start the process of creating and uploading the zip file according to the arguments passed to the constructor
*/
public void upload() throws IOException{
startProcess()
}
public static void main(String[] args) throws IOException {
new GetUploadFiles(args).upload();
}
private void startProcess() throws IOException{
//my code is here
//Can just use arguments in here since class is no longer static.
}
}
这会在一定程度上简化您的代码并标准化您对该类的使用。
关于java程序,使用main和外部调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32831045/