zoukankan      html  css  js  c++  java
  • 静态类

    今天写了一个例子。

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

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

  • 相关阅读:
    POJ 2386 Lake Counting
    POJ 1852 Ants
    HDU 4570 Multi-bit Trie
    HDU 4611 Balls Rearrangement
    ZOJ 3551 Bloodsucker
    HEU 百题解1001 谁是中间的那个
    BNU 1001 北师大ACM新手指导/ICPC introduction(1)
    BOJ 1580 Shoot
    BOJ 1578 Maximum
    BOJ 1577 Easy Game
  • 原文地址:https://www.cnblogs.com/edenpans/p/4558179.html
Copyright © 2011-2022 走看看