promotion image of download ymail app
Promoted
KK asked in 電腦與網際網路程式設計 · 1 decade ago

JAVA 解釋 chatBox = box;

public void setMessageObserver(ChatOneToOne box) {

chatBox = box;

}

public String getMessage() {

return message;

}

1 Answer

Rating
  • 酷兒
    Lv 5
    1 decade ago
    Favorite Answer

    你只有貼部份的程式碼, 但就你這幾行程式來看, setMessageObserver應該是某一個類別的方法, 作用是將外部物件傳入到這個類別當中, chatBox 在類別當中應該是被宣告成 ChatOneToOne 型別變數

    chatBox = box 就是將box物件傳進類別中

    下面是一個很簡單的範例, 模仿你目前的類別設計, 你可以自己編譯執行看看

    //TESTAP.java

    import java.io.*;

    class TESTAP {

    public static void main(String[] args) {

    TestClass tc= new TestClass();

    ChatOneToOne box = new ChatOneToOne ();

    box.Name = "Yahoo~";

    tc.setMessageObserver(box);

    System.out.println(tc.getName());

    }

    }

    //ChatOneToOne.java

    class ChatOneToOne{

    public String Name;

    public String ID;

    }

    //TestClass.java

    class TestClass{

    private ChatOneToOne chatBox;

    public void setMessageObserver(ChatOneToOne box) {

    chatBox = box;

    }

    public String getName(){

    return chatBox.Name;

    }

    }

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.