/*
錯誤原因:
main()函數是static即靜態
靜態是在類創建對象前就已經出現所以他只能調用同樣是靜態的函數
*/
class ArrayDemo1{
public static void main(String[] args){
int[] x={13,45,65,23,78,12};
show1(x);
}
//非static調用錯誤
public void show(int [] x){
for(int i=0;i<x.length;i++){
if(i==x.length-1){
System.out.println(x[i]);
}else{
System.out.print(x[i]+" ,");
}
}
}
//static函數調用沒問題
public static void show1(int [] x){
for(int i=0;i<x.length;i++){
if(i==x.length-1){
System.out.println(x[i]);
}else{
System.out.print(x[i]+" ,");
}
}
}
}
class ArrayDemo2{
public static void main(String[] args){
int[] x={13,45,65,23,78,12};
ArrayTool.show1(x);//靜態調用
//ArrayTool.show(x);//非法調用因爲show不是靜態函數
//---------------------------------//
/*
調用非靜態成員時候需要先創建對象
如果ArrayTool類定義了私有的構造函數就不能創建ArrayTool類的對象
private ArrayTool(){}
*/
ArrayTool at=new ArrayTool();
at.show(x);
}
}