实例化单例重载构造函数时遇到问题 - 错误:类型中的构造函数不能应用于给定类型

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

我只是想测试代码,无法创建 GoPiGo3 类的实例。我使用的是BlueJ,在BlueJ中将GautamV的代码打包,并将GoPiGo3类导入到一个demo类中。

我的研究使我相信 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 中具有私有访问权限


慕森卡
浏览 95回答 1
1回答

哔哔one

正如您所说,它是使用单例模式实现的,因此您需要使用Instance方法而不是构造函数。由于 constructor 上的 private 修饰符private GoPiGo3(int addr, boolean detect)...,它只能从 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/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java