我试图让这种方法来创建类'运动'的两个实例。该方法传入一个数组,其中包含有关该类的信息,然后将该数据发送给要创建的构造函数。如何从数组创建多个Java对象实例?
但是,我不确定如何引用实例1或2给定我的代码。
public static void seperateValues(String sportDetail) {
String[] sportDetails = sportDetail.split(",");
System.out.println("Adding new sport to the Sport collection");
System.out.println(sportDetail);
/*
for(int i=0; i<sportDetails.length; i++) //just used for testing whether it was splitting correctly {
System.out.println(sportDetails[i]);
}*/
//name,usagefee,insurance,affiliationfees, then court numbers
//Tennis,44,10,93,10,11,12,13,14,15,16
int vlength;
vlength = sportDetail.length();
new Sport(sportDetails); //this static methods loops twice, giving tennis and baseball
System.out.println(name); //this lists tennis and baseball respectively
}
“'//这个静态方法循环两次,给网球和棒球'”,你的意思是'seperateValues()'方法在其他地方被循环调用吗? –
哪个是你在评论中谈论的静态方法? –
如果所有这些方法最后都会创建一个对象,为什么不让它返回这个对象,以便随后可以将它引用到它所在的位置? –