關(guān)于方法名前面的static
在一下代碼中,為什么?public static void paiMing(int score[]){必須要有static (不然就會(huì)報(bào)錯(cuò))
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int [] scores = {89,-23,64,91,119,52,73};
? ? ? ??
? ? ? ? paiMing(scores);
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public static void paiMing(int score[]){
? ? ? ? int one,tow,three,length;
? ? ? ? length=score.length;
? ? ? ? Arrays.sort(score);
? ? ? ? one=score[length-1];
? ? ? ? tow=score[length-2];
? ? ? ? three=score[length-3];
? ? ? ? System.out.println("first "+one);
? ? ? ? System.out.println("second "+tow);
? ? ? ? System.out.println("third "+three);
? ? }
}
2016-01-22
paiMing(scores);這個(gè)地方你直接調(diào)用方法了 沒有通過對(duì)象調(diào)用
靜態(tài)的方法可以不通過對(duì)象直接調(diào)用 所以你加上static就不報(bào)錯(cuò)
普通的就必須通過對(duì)象調(diào)用 否則就像你看到的 就報(bào)錯(cuò)了
2016-01-22
? 你有毒