简体   繁体   English

尝试创建 Singleton 时出现 getInstance() 错误

[英]I am getting a getInstance() error occurs when trying to create a Singleton

I know there is lots online and I have been looking for the last two hours trying to fix my code.我知道网上有很多,我一直在寻找过去两个小时试图修复我的代码。

I am making a immutable class called Car I have seen lots of videos and read articles online.我正在制作一个名为 Car 的不可变 class 我看过很多视频并在线阅读文章。

Even my notes from college talk about creating a Singleton class when make a class Immutable (my notes are rubbish as usual).甚至我在大学的笔记也谈到了在制作 class 时创建一个 Singleton Immutable (我的笔记像往常一样是垃圾)。

Anyway the code below has everything I have seen in videos and read in articles the only difference is in everything I watched and read is their Objects did not have other variables.无论如何,下面的代码包含我在视频中看到的和在文章中阅读的所有内容,唯一的区别是我观看和阅读的所有内容是它们的对象没有其他变量。

public final class Car{
    private static Car carSingleton = null;
    private final String owner;
    private final String reg;
    private final String make;
    private final int kilometres;
    private final double price;



    private Car() {}    


    public static Car getInstance()() {
        if(carSingleton == null) {
            carSingleton = new Car();
        }   
        return carSingleton;
    }



    public String getOwner(){return owner;}
    public String getReg(){return reg;}
    public String getMake(){return make;}
    public int getKilometres(){return kilometres;}
    public double getPrice(){return price;}



}

I am getting a an error in the getInstance() constructor Car() is undefined.我在getInstance()构造函数中收到错误 Car() 未定义。

EDIT: Here is the actual question I was given编辑:这是给我的实际问题

Question 1 Given below is a mutable Car class.问题 1 下面给出的是可变汽车 class。 Re-write it so that it becomes immutable and write a simple test program.重新编写它,使其变得不可变并编写一个简单的测试程序。

class Car{
    private String owner;
    private String reg;
    private String make;
    private int kilometres;
    private double price;
    public Car(String owner, String reg, String make, int kilometres, double price){
            this.owner = owner; this.reg = reg;this.make = make; 
            this.kilometres = kilometres; this.price = price; 
    }
    public String owner(){return owner;}
    public String reg(){return reg;}
    public String make(){return make;}
    public intkilometres(){return kilometres;}
    public double price(){return price;}
    public void setPrice(double p){price = p;}
    public void setOwner(String ow){owner = ow;}
    public void setKil(int k){kilometres = k;}
    public void setMake(String m){make = m;}
}

In your getInstance() you are calling a constructor which is not defined:在您的getInstance()中,您正在调用未定义的构造函数:

carSingleton = new Car();

Define a default constructor to fix the error:定义一个默认构造函数来修复错误:

private Car() {
    // initialize final variables
}

or use some other constructor in getInstance()或在 getInstance() 中使用其他构造函数

Answering the EDIT part:回答编辑部分:

If a type (class) is immutable it only means you can't change its instances once you create them.如果一个类型(类)是不可变的,那只意味着一旦创建它们就不能更改它的实例。 Only thing you need to do in your assignment is remove all of the set methods and make all class attributes final .您在作业中唯一需要做的就是删除所有set方法并使所有 class 属性为final

Like you said, your notes are not helping you at all.就像你说的,你的笔记对你一点帮助都没有。 Singleton should not be used for this purpose. Singleton 不应用于此目的。

To make this class immutable remove the setters.要使此 class 不可变,请删除设置器。 It should not be Singleton.它不应该是 Singleton。

class Car{
    private String owner;
    private String reg;
    private String make;
    private int kilometres;
    private double price;
    public Car(String owner, String reg, String make, int kilometres, double price){
            this.owner = owner; this.reg = reg;this.make = make; 
            this.kilometres = kilometres; this.price = price; 
    }
    public String getOwner(){return owner;}
    public String getReg(){return reg;}
    public String getMake(){return make;}
    public getKilometres(){return kilometres;}
    public double getPrice(){return price;}
}

You can to try with this updates:您可以尝试使用此更新:

private static Car carSingleton  = new Car();

private Car() { 
   System.out.println(“creating”); 
   if (instance != null) { 
      throw new RuntimeException(“already exist!!! “); 
   } 
}

public static Car getInstance() { 
     return carSingleton; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试 setOnClickListener 时出现错误 - I am getting error when trying to setOnClickListener 尝试使用ViewPager和GridView在android中创建图像库时出现错误 - I am getting error when trying to create an image gallery in android using ViewPager and GridView 为什么在尝试创建JDOM文档时会收到DefferedDocumentImpl? - Why am I getting a DefferedDocumentImpl when trying to create a JDOM Document? 为什么在尝试创建此屏幕(窗口)时出现异常? - Why am I getting an exception when trying to create this Screen(window)? 为什么在尝试与我的服务器通信时出现此错误? - Why am i getting this error when trying to communicate with my server? 尝试使用 AWS CodeGuru 时出现此错误 - I am getting this error when trying to user AWS CodeGuru 我在尝试加载大于4的地图时遇到错误 - I am getting an Error when trying to load a map bigger than 4 当我试图解密获得以下错误 - When I am trying to decrypt getting following error 为什么在尝试实现 ViewModel 时出现此错误? - Why am i getting this error when trying to Implement ViewModel? How do I connect SceneBuilder to Netbeans 11, I am getting an error when trying to create a new Java ant JavaFX FXML Application - How do I connect SceneBuilder to Netbeans 11, I am getting an error when trying to create a new Java ant JavaFX FXML Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM