[英]trouble instantiating a singleton overloaded constructor -Error: constructor in type cannot be applied to given types
我正在使用github上GautamV / J4GPG的GoPiGo3类来控制DexterIndustries的GoPiGo3板。 该代码不是来自DexterIndustries的官方代码,而是来自DexterIndustries的python库的java端口。
我只是尝试测试代码,无法创建GoPiGo3类的实例。 我正在使用BlueJ,在BlueJ中打包了GautamV的代码,然后将GoPiGo3类导入了一个演示类。
我的研究使我相信GoPiGo3类被设计为一个单例,以确保仅创建一个实例,并具有重载的构造函数以允许其实例化具有灵活性。
以下是GoPiGo类中的相关代码:
private static GoPiGo3 _instance;
public static GoPiGo3 Instance() throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(8, true);
}
return _instance;
}
public static GoPiGo3 Instance(int addr) throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(addr, true);
}
return _instance;
}
public static GoPiGo3 Instance(boolean detect) throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(8, detect);
}
return _instance;
}
public static GoPiGo3 Instance(int addr, boolean detect) throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(addr, detect);
}
return _instance;
}
private GoPiGo3(int addr, boolean detect) throws IOException, FirmwareVersionException {
SPIAddress = addr;
spi = SpiFactory.getInstance(SpiChannel.CS1, // Channel 1
500000, // 500 kHz
SpiMode.MODE_0); // Mode 0
if (detect) {
//does detect stuff
}
预期结果是GoPiGo3类的初始化对象。 代码当前无法编译。 GoPiGo类的编译没有错误,但是尝试初始化GoPiGo类的Demo类却没有。
我的实例化尝试是
GoPiGo3 platform = new GoPiGo3();
这导致以下错误:
com.j4gpg3.control.GoPiGo3类中的构造方法GoPiGo3无法应用于给定类型:必需:int.boolean
找到:没有参数
原因:实际参数和形式参数列表的长度不同。您在此处使用的运算符不能用于要使用其的值的类型。 您在此处使用错误的类型或错误的运算符。
当我尝试:
GoPiGo3 platform = new GoPiGo3(8,true);
这导致以下错误:
GoPiGo3(int,boolean)在com.j4gpg3.control.GoPiGo3中具有私有访问权限
如您所说,其使用单例模式实现,因此您需要使用Instance
方法而不是构造函数。 由于构造函数private GoPiGo3(int addr, boolean detect)...
上的private修饰符,只能在GoPiGo3类中调用它。
public static GoPiGo3 Instance() throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(8, true);
}
return _instance;
}
public static GoPiGo3 Instance(int addr) throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(addr, true);
}
return _instance;
}
public static GoPiGo3 Instance(boolean detect) throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(8, detect);
}
return _instance;
}
public static GoPiGo3 Instance(int addr, boolean detect) throws IOException, FirmwareVersionException{
if (_instance == null) {
_instance = new GoPiGo3(addr, detect);
}
return _instance;
}
要获取GoPiGo3
实例,您需要执行以下操作:
GoPiGo3 platform = GoPiGo3.Instance(8,true);
参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.