2013-02-11 107 views
0

我已经启动JAVA并使用RxTx进行串行通信。'this'关键字:使用它作为参数

参考: 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/

在第二连杆我无法破译的使用 '这个': 任何人都可以请解释:

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.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"); 
     } 
    } 
.... 
} 

再次我感到困惑与使用的 '这个' 此处(serialPort.addEventListener(本);

我与代码 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/

的addEventListener

公共抽象无效的addEventListener(SerialPortEventListener LSNR) 抛出java.util.TooManyListenersException 注册一个SerialPortEventListener对象来侦听SerialEvents。对特定事件的兴趣可以使用notifyOnXXX调用表示。 SerialPortEventListener的serialEvent方法将用一个描述事件的SerialEvent对象来调用。

我想知道它是如何将'SerialPortEventListener lsnr'作为参数传递给上述代码中的addEventListener的使用。

由于

回答

1

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

现在让我们来看看您的使用情况: -

new Communicator(this); 

由于此语句用于GUI类的方法里面,所以,thisinstance of GUI,当前执行的代码。现在,通过将它传递给构造函数,您只需将当前实例的引用传递给它。这是相当有效的,因为Communicator构造函数采用GUI类型的引用: -

public Communicator(GUI window) 
{ 
    this.window = window; 
} 

现在让我们用下面的语句前进:

serialPort.addEventListener(this); 

在这里,你正在注册的serialPortEventListener,其被this引用。因为,这是在类内使用 - Communicator,它实现SerialPortEventListener,所以基本上你注册到一个Communicator实例,这只是一个SerialPortEventListener。所以,你正在注册该事件。

至于你的其他代码而言:

serialPort.addEventListener(new SerialReader(in)); 

在这里,你刚才使用的新instance代替this,因为你是不是里面SerialReader类。因此,您没有this对任何SerialReader实例的引用,因此您需要手动创建该类的对象。

所以,没有区别。因为无论如何,您只注册实施SerialPortEventListener的课程。

+0

我明白了。你能解释第二次使用serialPort.addEventListener(this); – 2013-02-11 16:25:17

+0

@GauravK。新增说明。 – 2013-02-11 16:27:29

+0

明白了。谢了哥们。 serialPort.addEventListener(new SerialReader(in));在Communicator类中,但它不实现SerialPortEventListener。我猜datz为什么这样表达。 – 2013-02-11 16:31:44

0

this是一个伪变量的含义(或在一个构造:“当前正在构造的对象”)“该方法被称为对物体”。我不知道你发布的代码存在什么问题,因为它包含了很多与代码无关的代码,我不会开始猜测你的问题到底在哪里。

0

在这里有很多东西要消化来回答你的原始问题,我不确定这一切都有关系,但我们来解决这个问题。下面的代码演示使用this在当前范围中删除歧义:

public Communicator(GUI window) 
{ 
    this.window = window; 
} 

this表示当前类的当前实例。所以在第一次混淆时,thisCommunicator的一个实例,因此存在this.window。在第二种情况下,thisGUI的一个实例,因此可以作为参数传递给Communicator,因为这是它所接受的。

Communicator的构造函数中,因为存在两个具有相同名称的东西,并且要在相同范围内使用,所以我们需要消除我们正在做的事情。我们通过说“将国外window东西分配给我们当地的,已知的,拥有的window东西来做到这一点。”

想想一群人,有许多约翰的人,而你只是在喊这个名字。谁会回应?如果你指出,或者有其他一些指标,比如正确的约翰,当他们看起来都认出你时呢? this就是这个指标,给出了与另一个不同的东西所需的特异性。

另外想象一下试图将window分配到window的混淆。我们是指将本地版本分配给本地版本,将参数分配给参数,将参数分配给本地版本还是将本地版本分配给参数?

+0

我完全明白你在说什么。我想通过突出显示的行来了解'this'的用法。 communicator = new Communicator(this); serialPort.addEventListener(this); – 2013-02-11 16:21:13

+0

@GauravK我刚刚在上下文中添加了一点关于'this'的内容。 – 2013-02-11 16:22:07