【问题标题】:trouble instantiating a singleton overloaded constructor -Error: constructor in type cannot be applied to given types实例化单例重载构造函数的麻烦-错误:类型中的构造函数不能应用于给定类型
【发布时间】:2019-07-02 15:31:39
【问题描述】:

我正在使用 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 中具有私有访问权限

【问题讨论】:

    标签: java singleton bluej constructor-overloading private-constructor


    【解决方案1】:

    正如您所说,它使用单例模式实现,因此您需要使用 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);
    

    参考:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2019-10-05
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多