gpt4 book ai didi

java - 实例化单例重载构造函数时遇到问题 - 错误 : constructor in type cannot be applied to given types

转载 作者:行者123 更新时间:2023-12-02 03:36:45 26 4
gpt4 key购买 nike

我正在使用 GautamV/J4GPG 中的 GoPiGo3 类在 github 上控制 DexterIndustries 的 GoPiGo3 板。该代码不是 DexterIndustries 的官方代码,而是 DexterIndustries 制作的 python 库的 java 端口。

我只是想测试代码,无法创建 GoPiGo3 类的实例。我使用的是BlueJ,在BlueJ中对GautamV的代码进行了封装,并将GoPiGo3类导入到演示类中。

我的研究使我相信 GoPiGo3 类被设计为单例,以确保只创建一个实例,并且具有重载构造函数以允许其实例化的灵 active 。

以下是 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 平台 = new GoPiGo3();

这会导致以下错误:

Constructor GoPiGo3 in class com.j4gpg3.control.GoPiGo3 cannot be applied to given types: required: int.boolean
found:no arguments
reason: actual and formal argument lists differ in length The operator that you use here cannot be used for the type of value that you are using it for. You are either using the wrong type here, or the wrong operator.

当我尝试时:

GoPiGo3 平台 = new GoPiGo3(8,true);

这会导致以下错误:

GoPiGo3(int,boolean) has private access in com.j4gpg3.control.GoPiGo3

最佳答案

正如您所说,它使用单例模式实现,因此您需要使用Instance 方法而不是构造函数。由于构造函数 private GoPiGo3(int addr, boolean detector)... 上的 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);

引用:

https://www.geeksforgeeks.org/singleton-class-java/

关于java - 实例化单例重载构造函数时遇到问题 - 错误 : constructor in type cannot be applied to given types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com