gpt4 book ai didi

java - “这个”关键字 : Using it as parameter

转载 作者:行者123 更新时间:2023-12-01 14:51:17 25 4
gpt4 key购买 nike

我已经启动了JAVA并使用RxTx进行串行通信。

引用: http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication http://henrypoon.wordpress.com/2011/01/01/serial-communication-in-java-with-example-program/

在第二个链接中,我无法破译'this'的用法:谁能解释一下:

Communicator.java

public class Communicator implements SerialPortEventListener
{
GUI window = null;
..
..
public Communicator(GUI window)
{
this.window = window;
}

...
..
}

在 GUI.java 中

public class GUI extends javax.swing.JFrame {
Communicator communicator = null;
Communicator communicator = null;
//KeybindingController object
KeybindingController keybindingController = null;

/** Creates new form GUI */
public GUI() {
initComponents();
createObjects();
communicator.searchForPorts();
keybindingController.toggleControls();
keybindingController.bindKeys();
}

private void createObjects()
{
**communicator = new Communicator(this);**
keybindingController = new KeybindingController(this);
}
...
..}

我很困惑如何使用它来创建 Communicator 类的对象,如上面代码中突出显示的(出现 communicator = new Communicator(this);)

另一个令人困惑的是:Communicator.java

public class Communicator implements SerialPortEventListener
{
...
...
public void connect()
{
String selectedPort = (String)window.cboxPorts.getSelectedItem();
selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);

CommPort commPort = null;

try
{
//the method below returns an object of type CommPort
commPort = selectedPortIdentifier.open("TigerControlPanel", TIMEOUT);
//the CommPort object can be casted to a SerialPort object
serialPort = (SerialPort)commPort;
....
...}


public void initListener()
{
try
{
**serialPort.addEventListener(this);**
serialPort.notifyOnDataAvailable(true);
}
catch (TooManyListenersException e)
{
logText = "Too many listeners. (" + e.toString() + ")";
window.txtLog.setForeground(Color.red);
window.txtLog.append(logText + "\n");
}
}
....
}

我再次对这里“this”的使用感到困惑(serialPort.addEventListener(this);)

我与以下代码进行了比较 http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication

那里有建议

...
InputStream in = serialPort.getInputStream();
**serialPort.addEventListener(new SerialReader(in));**
...

public static class SerialReader implements SerialPortEventListener
{
private InputStream in;
private byte[] buffer = new byte[1024];

public SerialReader ( InputStream in )
{
this.in = in;
}

public void serialEvent(**SerialPortEvent arg0**) {
int data;

try
{
int len = 0;
while ( ( data = in.read()) > -1 )
{
if ( data == '\n' ) {
break;
}
buffer[len++] = (byte) data;
}
System.out.print(new String(buffer,0,len));
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
}

}

addEventListener的说明 http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/

添加事件监听器

公共(public)抽象无效addEventListener(SerialPortEventListener lsnr) 抛出 java.util.TooManyListenersException注册 SerialPortEventListener 对象来监听 SerialEvent。可以使用notifyOnXXX 调用来表达对特定事件的兴趣。 SerialPortEventListener 的serialEvent 方法将通过描述事件的SerialEvent 对象来调用。

我想知道它的用法,因为它如何将“SerialPortEventListener lsnr”作为参数传递给上面代码中的 addEventListener。

谢谢

最佳答案

this 关键字是对正在执行代码的当前实例的引用。因此,由于 this 是一个引用,因此您可以将其用作任何其他引用。没问题。

现在让我们看看您的用法:-

new Communicator(this);

由于该语句是在GUI类的方法内部使用的,因此,this指的是当前正在执行代码的GUI实例 。现在,通过将其传递给构造函数,您只需将当前实例的引用传递给它即可。这是非常有效的,因为 Communicator 构造函数采用 GUI 类型的引用: -

public Communicator(GUI window)
{
this.window = window;
}
<小时/>

现在让我们继续下一个语句:

serialPort.addEventListener(this);

在这里,您将使用 this 引用的 EventListener 注册 serialPort。因为,这是在类 Communicator 中使用的,它实现了 SerialPortEventListener,所以基本上你是在注册一个 Communicator 实例,它只不过是一个SerialPortEventListener。因此,您正在注册该 Activity 。

就您的其他代码而言:

serialPort.addEventListener(new SerialReader(in));

在这里,您刚刚使用了一个新的实例而不是this,因为您不在SerialReader类中。因此,您没有对任何 SerialReader 实例的 this 引用,因此您需要手动创建该类的对象。

所以,没有什么区别。因为,无论如何,您只是注册实现 SerialPortEventListener 的类。

关于java - “这个”关键字 : Using it as parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com