码迷,mamicode.com
首页 > 其他好文 > 详细

静态类

时间:2015-06-07 12:28:58      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

今天写了一个例子。

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,这个时候就不会报错。

具体原因我想是编译器编译顺序的问题。后面可以在查查资料看看具体原因。

静态类

标签:

原文地址:http://www.cnblogs.com/edenpans/p/4558179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!