创建静态工厂
Creating a static factory
我正在制作一个应用程序来控制串行连接的打印机,我想提供一个静态工厂方法 PrinterLocator.FindPrinters()
,它将 return 一组可用打印机连接到我的系统。
例如,Serial 库提供了 Serial.list()
,这是一个静态方法 returning 一个字符串数组,对应于我系统中可用的端口。我正在尝试创建类似的东西,但出现错误 "No enclosing instance of type SLPDriver is accessible. Must qualify the allocation with an enclosing instance of type SLPDriver"
实现这个设计模式的正确方法是什么?
SLP驱动程序:
SerialPrinter myPrinter;
void setup()
{
SerialPrinter[] availablePrinters = PrinterLocator.FindPrinters();
if(availablePrinters.length > 0)
{
myPrinter = availablePrinters[0];
}
}
void draw()
{
}
串行打印机:
import processing.serial.*;
static class PrinterLocator
{
static final int baudRates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200};
static final int baudCount = baudRates.length;
static SerialPrinter[] FindPrinters()
{
SerialPrinter[] foundPrinters, tempPrinters;
foundPrinters = new SerialPrinter[0];
String[] foundPorts = Serial.list();
int numPorts = foundPorts.length;
int numPrintersFound = 0;
if(numPorts <= 0) return foundPrinters;
SerialPrinter testPrinter;
tempPrinters = new SerialPrinter[numPorts];
for(int i = 0; i < numPorts; i++)
{
for(int b = 0; b < baudCount; b++)
{
testPrinter = new SerialPrinter("test", foundPorts[i], baudRates[b]);
if(testPrinter.IsValid())
{
tempPrinters[numPrintersFound] = testPrinter;
numPrintersFound++;
break;
}
}
}
if(numPrintersFound > 0)
{
foundPrinters = new SerialPrinter[numPrintersFound];
for(int i = 0; i < numPrintersFound; i++)
{
foundPrinters[i] = tempPrinters[i];
}
}
return foundPrinters;
}
}
class SerialPrinter
{
//Members
private Serial myPort;
private String printerName;
private boolean valid;
private String portName;
private int baudRate;
public SerialPrinter()
{
this("","",0);
}
public SerialPrinter(String name, String port, int baud)
{
printerName = name;
Configure(port, baud);
}
public boolean IsValid()
{
return valid;
}
public boolean Configure(String port, int baud)
{
print("Configuring Printer ");
print(port);
print("@");
print(baud);
print(": ");
try
{
myPort.stop();
myPort = null;
}
catch (Exception e) {}
portName = port;
baudRate = baud;
try
{
myPort = new Serial(this, port, baud);
myPort.clear();
myPort.write(0xA5);
int timeout = millis() + 1000;
while((millis() < timeout) && (myPort.available() == 0)) { }
if(myPort.available() > 0)
{
int inByte = myPort.read();
if(inByte == 0xC9)
{
valid = true;
}
else
{
valid = false;
}
}
else
{
valid = false;
}
}
catch (Exception e)
{
valid = false;
}
if(valid)
{
println("[OK]");
}
else
{
println("[ERR]");
}
return valid;
}
}
No enclosing instance of type SLPDriver is accessible. Must qualify the allocation with an enclosing instance of type SLPDriver
错误表明 SerialPrinter
是 SLPDriver
的内部 class。
您需要将 class 声明更改为静态:
static class SerialPrinter
{
...
或者如果它应该是内部的,你需要使用一个 SLPDriver
实例来创建一个 SerialPrinter
:
someSLPDriver.new SerialPrinter(...)
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Serial
构造函数似乎需要 PApplet
的实例。实例化表达式也应该给你一个错误,因为 this
指的是 SerialPrinter
,而不是 PApplet
.
您可以尝试如下操作:
...
static class PrinterLocator {
...
static SerialPrinter[] FindPrinters(SLPDriver context) {
...
... = context.new SerialPrinter(...);
}
}
class SerialPrinter {
...
public boolean Configure(...) {
...
... = new Serial(PApplet.this, ...);
}
}
在小程序上下文中调用FindPrinters
时,需要像
这样调用
... = PrinterLocator.FindPrinters(this);
我正在制作一个应用程序来控制串行连接的打印机,我想提供一个静态工厂方法 PrinterLocator.FindPrinters()
,它将 return 一组可用打印机连接到我的系统。
例如,Serial 库提供了 Serial.list()
,这是一个静态方法 returning 一个字符串数组,对应于我系统中可用的端口。我正在尝试创建类似的东西,但出现错误 "No enclosing instance of type SLPDriver is accessible. Must qualify the allocation with an enclosing instance of type SLPDriver"
实现这个设计模式的正确方法是什么?
SLP驱动程序:
SerialPrinter myPrinter;
void setup()
{
SerialPrinter[] availablePrinters = PrinterLocator.FindPrinters();
if(availablePrinters.length > 0)
{
myPrinter = availablePrinters[0];
}
}
void draw()
{
}
串行打印机:
import processing.serial.*;
static class PrinterLocator
{
static final int baudRates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200};
static final int baudCount = baudRates.length;
static SerialPrinter[] FindPrinters()
{
SerialPrinter[] foundPrinters, tempPrinters;
foundPrinters = new SerialPrinter[0];
String[] foundPorts = Serial.list();
int numPorts = foundPorts.length;
int numPrintersFound = 0;
if(numPorts <= 0) return foundPrinters;
SerialPrinter testPrinter;
tempPrinters = new SerialPrinter[numPorts];
for(int i = 0; i < numPorts; i++)
{
for(int b = 0; b < baudCount; b++)
{
testPrinter = new SerialPrinter("test", foundPorts[i], baudRates[b]);
if(testPrinter.IsValid())
{
tempPrinters[numPrintersFound] = testPrinter;
numPrintersFound++;
break;
}
}
}
if(numPrintersFound > 0)
{
foundPrinters = new SerialPrinter[numPrintersFound];
for(int i = 0; i < numPrintersFound; i++)
{
foundPrinters[i] = tempPrinters[i];
}
}
return foundPrinters;
}
}
class SerialPrinter
{
//Members
private Serial myPort;
private String printerName;
private boolean valid;
private String portName;
private int baudRate;
public SerialPrinter()
{
this("","",0);
}
public SerialPrinter(String name, String port, int baud)
{
printerName = name;
Configure(port, baud);
}
public boolean IsValid()
{
return valid;
}
public boolean Configure(String port, int baud)
{
print("Configuring Printer ");
print(port);
print("@");
print(baud);
print(": ");
try
{
myPort.stop();
myPort = null;
}
catch (Exception e) {}
portName = port;
baudRate = baud;
try
{
myPort = new Serial(this, port, baud);
myPort.clear();
myPort.write(0xA5);
int timeout = millis() + 1000;
while((millis() < timeout) && (myPort.available() == 0)) { }
if(myPort.available() > 0)
{
int inByte = myPort.read();
if(inByte == 0xC9)
{
valid = true;
}
else
{
valid = false;
}
}
else
{
valid = false;
}
}
catch (Exception e)
{
valid = false;
}
if(valid)
{
println("[OK]");
}
else
{
println("[ERR]");
}
return valid;
}
}
No enclosing instance of type SLPDriver is accessible. Must qualify the allocation with an enclosing instance of type SLPDriver
错误表明 SerialPrinter
是 SLPDriver
的内部 class。
您需要将 class 声明更改为静态:
static class SerialPrinter
{
...
或者如果它应该是内部的,你需要使用一个 SLPDriver
实例来创建一个 SerialPrinter
:
someSLPDriver.new SerialPrinter(...)
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Serial
构造函数似乎需要 PApplet
的实例。实例化表达式也应该给你一个错误,因为 this
指的是 SerialPrinter
,而不是 PApplet
.
您可以尝试如下操作:
...
static class PrinterLocator {
...
static SerialPrinter[] FindPrinters(SLPDriver context) {
...
... = context.new SerialPrinter(...);
}
}
class SerialPrinter {
...
public boolean Configure(...) {
...
... = new Serial(PApplet.this, ...);
}
}
在小程序上下文中调用FindPrinters
时,需要像
... = PrinterLocator.FindPrinters(this);