Java程序的参数传递及项目打包成JAR文件
(以内部类为例)
1.运行Java程序时,可以给程序传递参数值,参数值存放在main方法的args参数中,其中args[0]存放第1个参数,args[1]存放第2个参数,依此类推。下面以内部类为例,说明Java程序的参数传递方法。
(1)示例程序如下:
package ylu.zgj;
public class School {
public static class Teacher{
int bh;
String xm;
Teacher(int bh,String xm){
this.bh=bh;
this.xm=xm;
}
}
public class Student{
int xh;
String xm;
Student(int xh,String xm){
this.xh=xh;
this.xm=xm;
}
}
public static void main(String[] args) {
School myApp=new School();
Student stu=myApp.new Student(12001,"张三同学"); //创建内部类对象
Teacher tea=new Teacher(1102,"李四老师"); //创建静态内部类对象
int bh=Integer.parseInt(args[0]);
School.Teacher tea2=new School.Teacher(bh, args[1]);//运行时传递参数值
System.out.println(stu.xh +stu.xm+" "+tea.bh+tea.xm+"
"+tea2.bh+tea2.xm);
}
}
(2)运行该程序时,需要给程序传递两个参数值,并使用这两个参数值创建对象tea2。如果在命令行下运行程序,输入的命令如下: