尝试创建单例时出现 getInstance() 错误
I am getting a getInstance() error occurs when trying to create a Singleton
我知道网上有很多,我一直在寻找最后两个小时来修复我的代码。
我正在制作一个名为 Car 的不可变 class 我在网上看过很多视频和文章。
即使是我的大学笔记也谈到在创建 class Immutable
时创建一个 Singleton class(我的笔记一如既往地垃圾)。
无论如何,下面的代码包含我在视频和文章中看到的所有内容,唯一的区别是我观看和阅读的所有内容是它们的对象没有其他变量。
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;}
}
我在 getInstance()
构造函数中遇到错误 Car() 未定义。
编辑:
这是我收到的实际问题
问题一
下面给出的是可变汽车 class。重新写成不可变的,写一个简单的测试程序。
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;}
}
在您的 getInstance()
中,您正在调用一个未定义的构造函数:
carSingleton = new Car();
定义一个默认构造函数来修复错误:
private Car() {
// initialize final variables
}
或在 getInstance()
中使用其他构造函数
回答编辑部分:
如果一个类型 (class) 是不可变的,它只意味着一旦创建实例就无法更改。您在作业中唯一需要做的就是删除所有 set 方法并使所有 class 属性成为 final
.
就像你说的,你的笔记对你一点帮助都没有。单例不应用于此目的。
要使此 class 不可变,请移除设置器。应该不是单例。
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;}
}
我知道网上有很多,我一直在寻找最后两个小时来修复我的代码。
我正在制作一个名为 Car 的不可变 class 我在网上看过很多视频和文章。
即使是我的大学笔记也谈到在创建 class Immutable
时创建一个 Singleton class(我的笔记一如既往地垃圾)。
无论如何,下面的代码包含我在视频和文章中看到的所有内容,唯一的区别是我观看和阅读的所有内容是它们的对象没有其他变量。
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;}
}
我在 getInstance()
构造函数中遇到错误 Car() 未定义。
编辑: 这是我收到的实际问题
问题一 下面给出的是可变汽车 class。重新写成不可变的,写一个简单的测试程序。
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;}
}
在您的 getInstance()
中,您正在调用一个未定义的构造函数:
carSingleton = new Car();
定义一个默认构造函数来修复错误:
private Car() {
// initialize final variables
}
或在 getInstance()
中使用其他构造函数回答编辑部分:
如果一个类型 (class) 是不可变的,它只意味着一旦创建实例就无法更改。您在作业中唯一需要做的就是删除所有 set 方法并使所有 class 属性成为 final
.
就像你说的,你的笔记对你一点帮助都没有。单例不应用于此目的。
要使此 class 不可变,请移除设置器。应该不是单例。
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;}
}