【问题标题】:java.lang.Error: Unresolved compilation problem for singelton classjava.lang.Error:未解决的单例类编译问题
【发布时间】:2019-08-27 13:15:19
【问题描述】:

我有一个程序有两个声明为单音类的类。 当我尝试运行程序时,我得到一个 java.lang.Error: Unresolved compiler problem 在获取单音类实例的行中,我不明白为什么。

错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at Model.Recommender.getInstance(Recommender.java:22)
    at Model.Library.<clinit>(Library.java:21)
    at Controller.MainClass.main(MainClass.java:69)

Recommender 类中的错误行:

public static Recommender getInstance() {

而类是这样声明的:

public class Recommender {

    private static Recommender recommender; //a recommender
    private HashSet<Reader> libraryReaders; //a hash set of all of the library readers

    //constructor to initialize the fields
    private Recommender() 
    {
        libraryReaders = new HashSet<Reader>();
    }

    //method to make the class a singleton class
    public static Recommender getInstance() {
        if(recommender == null) //check that a instance of this class doesn't exist
        {
            recommender = new Recommender(); //creates a new instance
        }
        return recommender; //return the instance that was created
    }
}

库类中的错误行:

private static Recommender recommender = Recommender.getInstance();

而类是这样声明的:

public class Library {

    private static Library library; //a library
    private static Recommender recommender = Recommender.getInstance();
    private HashSet<Reader> readers;
    private HashSet<Author> authors;
    private HashSet<LibraryItem> items;

    //constructor to initialize the fields
    private Library() {
        readers = new HashSet<Reader>();
        authors = new HashSet<Author>();
        items = new HashSet<LibraryItem>();
    }

    //method to make the class a singleton class
    public static Library getInstance() {
        if(library == null) //check that a instance of this class doesn't exist
        {
            library = new Library(); //creates a new instance
        }
        return library; //return the instance that was created
    }
}

Main 类中的错误:

LibrarySys = Library.getInstance();

我正在库类中创建一个推荐器实例,并在主类中创建一个库实例。为什么会出现此错误?

【问题讨论】:

  • 您的方法名称是Recommender.getInstance(),您调用的是Recommender.getInstance1()。我认为只是一个拼写错误;)
  • 我改了,还是一样的错误
  • @Daniel16 我无法重现您的问题,我复制了相同的代码,但没有得到任何东西。您能否添加更多信息,例如包和导入名称?
  • 注意:“未解决的编译问题”表示代码无法正确编译。当 Eclipse 在代码中显示错误时,您无法干净地运行代码。

标签: java eclipse compiler-errors


【解决方案1】:

您发布的代码很好。至于您的编译错误,您必须检查您的导入,以及其他未在此代码中编写的类,例如 Reader、Author 和 LibraryItem。您在应用程序中使用外部 JAR 吗?也许你的类路径是问题。

此外,在 2019 年,您的 Recommender 单例类可以简单地编写为枚举,而无需 getInstance() 方法(无论如何这不是线程安全的):

public enum Recommender {

INSTANCE;

private HashSet<Reader> libraryReaders;

private Recommender() {
    libraryReaders = new HashSet<Reader>();
}

//getters and setters... //

}

使用它就像编写 Recommender.INSTANCE 一样简单,无需初始化任何东西。

【讨论】:

    【解决方案2】:

    什么是图书馆系统? 不应该吗

    Library myLibrary = Library.getInstance();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-03
      • 2014-01-24
      • 2014-05-08
      • 2017-07-16
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多