【发布时间】: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