trouble instantiating a singleton overloaded constructor -Error: constructor in type cannot be applied to given types

trouble instantiating a singleton overloaded constructor -Error: constructor in type cannot be applied to given types

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

我只是想测试代码,无法创建 GoPiGo3 的实例 class。我正在使用 BlueJ,在 BlueJ 中打包了 GautamV 的代码,并将 GoPiGo3 class 导入到演示中 class.

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

这里是GoPiGo的相关代码class:


    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的初始化对象class。 代码目前无法编译。 GoPiGo class 编译没有错误,但是尝试初始化 GoPiGo class 的 Demo class 没有。

我的实例化尝试是

GoPiGo3 platform = 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 platform = new GoPiGo3(8,true);

这会导致以下错误:

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

正如您所说,它是使用单例模式实现的,因此您需要使用 Instance 方法而不是构造函数。由于构造函数private GoPiGo3(int addr, boolean detect)...上的私有修饰符,它只能从GoPiGo3 class.

内部调用
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/