我試圖讓這種方法來創建類'運動'的兩個實例。該方法傳入一個數組,其中包含有關該類的信息,然後將該數據發送給要創建的構造函數。如何從數組創建多個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()'方法在其他地方被循環調用嗎? –
哪個是你在評論中談論的靜態方法? –
如果所有這些方法最後都會創建一個對象,爲什麼不讓它返回這個對象,以便隨後可以將它引用到它所在的位置? –