2013-02-11 111 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