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

标签 java singleton bluej constructor-overloading private-constructor

我正在使用 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/

相关文章:

jakarta-ee - 迁移/创建 HA-Singleton 到 jboss 7

javascript - 这是单例模式吗

ios - 核心数据上下文和单例数据 Controller

java - 如何从 ArrayList 中删除存储在外部 txt 文件中的项目

java - 将自纪元以来的毫秒时间转换为 "mm/dd/yy"

java - 无法通过java连接oracle 11g

java - 多个条件的 while 循环

java - 定时器延迟不起作用?

java - 如何从其他类方法访问 ConcurrentHashMap 元素

java - HttpURLConnection 实现中的 "Socket sharing"