当前位置: 首页 > 工具软件 > OFTest > 使用案例 >

Java出现No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosin

司徒兴德
2023-12-01

最近研究LDA源代码时,里面涉及到Comparable方法的使用。以前用过这个排序方法,现在想回顾一下。以下是程序,感觉没问题啊,结果报错了:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).

以下是代码程序:

package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    /**
     * 根据order对User排序
     */
    public  class Student implements Comparable<Student>{
        private String name;
        private Integer grade;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getGrade() {
            return grade;
        }
        public void setGrade(Integer grade) {
            this.grade = grade;
        }
        public int compareTo(Student arg0) {
            return this.getGrade().compareTo(arg0.getGrade());
        }
    }
    public static void main(String[] args) {

        Student student1 = new Student();
        student1.setName("a");
        student1.setGrade(80);
        Student student2 = new Student();
        student2.setName("b");
        student2.setGrade(59);
        List<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        Collections.sort(list);
        for(Student u : list){
            System.out.println(u.getName());
        }

    }

}

于是百度谷歌了一下相关资料。原来我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以在不做其他变动的情况下,最简单的解决办法是将public class改为public static class.

package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    /**
     * 根据order对User排序
     */
    public static class Student implements Comparable<Student>{
        private String name;
        private Integer grade;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public Integer getGrade() {
            return grade;
        }
        public void setGrade(Integer grade) {
            this.grade = grade;
        }
        public int compareTo(Student arg0) {
            return this.getGrade().compareTo(arg0.getGrade());
        }
    }
    public static void main(String[] args) {

        Student student1 = new Student();
        student1.setName("a");
        student1.setGrade(80);
        Student student2 = new Student();
        student2.setName("b");
        student2.setGrade(59);
        List<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        Collections.sort(list);
        for(Student u : list){
            System.out.println(u.getName());
        }

    }

}

再次运行,发现没问题了。

 类似资料: