//這是一個小的代碼,研究如何收集的作品,但是當我創建的主要方法的山對象時,它給出了一個錯誤。最後進一步解釋。不是允許在main方法聲明
import java. util. * ;
public class sortMountains {
class Mountain {
String name;
int height;
Mountain(String n, int h) {
name = n;
height = h;
}
public String toString() {
return name + + height;
}
}
List<Mountain> mtn = new ArrayList<Mountain>();
class NameCompare implements Comparator <Mountain> {
public int compare(Mountain one, Mountain two) {
return one.name. compareTo(two. name);
}
}
class HeightCompare implements Comparator <Mountain> {
public int compare(Mountain one, Mountain two) {
return (two. height - one. height) ;
}
}
public void go() {
mtn.add(new Mountain("Longs ", 14255));
mtn.add(new Mountain("Elbert ", 14433));
mtn.add(new Mountain("Maroon " , 14156));
mtn.add(new Mountain("Castle ", 14265));
System.out.println("as entered:\n" + mtn);
NameCompare nc = new NameCompare();
Collections.sort(mtn, nc);
System.out.println("by name:\n'" + mtn);
HeightCompare hc = new HeightCompare();
Collections.sort(mtn, hc);
System.out.println("by height:\n " + mtn);
}
public static void main(String args[]){
sortMountains sorting=new sortMountains();
sorting.go();
//error line Mountain a=new Mountain("Everest",12121);
}
}
以上時沒有錯誤線工作正常,但當我想創建山的對象主要方法它提供了一個錯誤「非靜態的不能從靜態方法中引用」
** ** 1搜索(HTTP [非靜態的不能從靜態方法中引用] .com/search?q =非靜態+不能+ be + reference + from + static + method&submit = search)** 2。**閱讀一些帖子/答案** 3。**避免-1's – 2012-11-22 07:51:40
http:// stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static(百萬分之一,無論如何都有很多upvotes) – 2012-11-22 07:53:58