最近研究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());}}}

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

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

  1. [Java]Error:No enclosing instance of type E is accessible. Must qualify the allocation with an enclo

    在自己试着写一个链表的例子时,由于参考的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must ...

  2. No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc

    java : No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ...

  3. No enclosing instance of type FormDetailBean is accessible. Must qualify the allocation with an encl

    最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...

  4. 语法错误:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    错误 问题 No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing 没 ...

  5. No enclosing instance of type TextRunnable is accessible. Must qualify the allocation with an enclo

    今天写demo的时候,遇到一个问题,关于内部类的,这里写一下 错误信息:No enclosing instance of type TextRunnable is accessible. Must q ...

  6. No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing i...

    最近在刷AC,在编译时出现: No enclosing instance of type Main is accessible. Must qualify the allocation with an ...

  7. No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing

    No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing ...

  8. No enclosing instance of type X is accessible. Must qualify the allocation with an enclosing instanc

    No enclosing instance of type X is accessible. Must qualify the allocation with an enclosing instanc ...

  9. No enclosing instance of type JDStudent is acAcessible. Must qualify the allocation with an enclosin

    No enclosing instance of type JDStudent is acAcessible. Must qualify the allocation with an enclosin ...

最新文章

  1. Linux——Linux概念架构的理解
  2. 获取AppStore上架后的应用版本号
  3. ajax跨域请求的问题
  4. python中shift函数rolling_Pandas Shift函数的基础入门学习笔记
  5. win10安装迅雷精简版处理方法---发布者不受信任
  6. Git可视化工具GitKraKen基本使用
  7. 【CGAL_空间搜索与排序】3D快速求交和距离计算
  8. 用VBA检查Word文档中是否存在位于行首的脚注引用,如存在则通过调整字符间距使其移动到非行首的位置
  9. Qt图像处理技术二:对QImage图片简单滤镜(暖色,冷色,反色,老照片,灰度)
  10. c语言浮典型数据类型,C语言的数据类型→浮点型数据
  11. 互联网岳麓峰会从长沙走向世界
  12. docker安装青龙面板薅羊毛(新手教程,大佬可略过)
  13. ubuntu共享文件夹不显示及设备空间不足解决[随手笔记]
  14. breezy,dapper,edgy,feisty的含义
  15. Spring Boot配置ssl证书
  16. 零基础学习深度学习_深度学习的基础!!!
  17. 小白兔写话_一年级小白兔看图写话
  18. 张柏芝《影子爱人》曝剧照 亲手设浪漫爱情桥段_0
  19. 深度linux桌面卸载wps,安装,卸载WPS-office报错,如何解决?
  20. 『网络安全』蜜罐到蜜网入门指南(三)蜜罐内部组成分析

热门文章

  1. 【WPF】如何保存RichTextBox的文本到数据库?以及如何对RichTextBox的Document做绑定?...
  2. 面试:MySQL InnoDB 事务隔离
  3. SpringBoot 计划任务
  4. 如何在六个月或更短的时间内成为DevOps工程师(一)
  5. Java 基础数据结构介绍
  6. vscode常用快捷键大全
  7. C#开发笔记之13-如何用C#分隔字符串并返回字符串数组?
  8. angular 模块构建_如何通过11个简单的步骤从头开始构建Angular 8应用
  9. 敏捷开发需求文档_需求的长期,敏捷文档
  10. 机器学习均方误差_机器学习:均方误差和回归线简介