亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

實例化單例重載構造函數時遇到問題 - 錯誤:類型中的構造函數不能應用于給定類型

實例化單例重載構造函數時遇到問題 - 錯誤:類型中的構造函數不能應用于給定類型

慕森卡 2023-03-02 16:02:34
我正在使用 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 中具有私有訪問權限
查看完整描述

1 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

正如您所說,它是使用單例模式實現的,因此您需要使用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/


查看完整回答
反對 回復 2023-03-02
  • 1 回答
  • 0 關注
  • 88 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號