//这是一个小的代码,研究如何收集的作品,但是当我创建的主要方法的山对象时,它给出了一个错误。最后进一步解释。不是允许在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