今天写了一个例子。
public static void main(String[] args){ SortedSet<Person> set = new TreeSet<Person>(); set.add(new Person(180)); set.add(new Person(175)); for(Person p : set){ System.out.println("身高: " + p.getHeight()); } }
然后在同样文件里面定义
class Person implements Comparable<Person>{ private int height; public Person(int _age){ height = _age; } public int getHeight(){ return this.height; } public void setHeight(int height){ this.height = height; } @Override public int compareTo(Person o){ return height - o.height; }
这个时候 编译器会报错:
No enclosing instance of type listSort is accessible. Must qualify the allocation with an enclosing instance of type listSort (e.g. x.new A() where x is an instance of listSort).
但是如果我把person类单独拿出来作为一个class,这个时候就不会报错。
具体原因我想是编译器编译顺序的问题。后面可以在查查资料看看具体原因。