简体   繁体   中英

access object method from another class

I'm new to Java and oriented-object and I'm trying to create a chat program. Here's what I'm trying to do:

Somewhere in my Main.java

Window window = new Window;

Somewhere in my Window.java

History history = new History()

Somewhere in my History.java:

public History()
{
    super(new GridBagLayout());

    historyArea = new JTextArea(15, 40);
    historyArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(historyArea);

    /* some other code... */
}

public void actionPerformed(ActionEvent event)
{
    String text = entryArea.getText();
    historyArea.append(text + newline);
    entryArea.selectAll();
    historyArea.setCaretPosition(historyArea.getDocument().getLength());
}

public JTextArea getHistoryArea()
{
    return historyArea;
}

public void addToHistoryArea(String pStringToAdd)
{
    historyArea.append(pStringToAdd + newline);
    historyArea.setCaretPosition(historyArea.getDocument().getLength());
}

Now that I'm in Server.java, I want to use the method addToHistoryArea. How can I do that without making my historyArea static? Because if I understand well how static works, I couldn't have different historyArea even if I create a new History...

Thanks for your help and tell me if I got it all wrong!

In your Server constructor, send the instance of your History object (eg new Server (history) , and then you can invoke, history.addToHistoryArea , other option would be have a setter method which sets an instance of history to an instance variable, and then just call the addToHistoryArea method

public class Server{

    private History history;

    public Server(History history){
        this.history = history;
    }

    public void someMethod(){
        this.history.addToHistoryArea();
    }
}

Another way

public class Server{

    private History history;

    public void setHistory(History history){
        this.history = history;
    }

    public void someMethod(){
        this.history.addToHistoryArea();
    }
}

In someplace in Server you can have History

public class Server{

    private History history;


    public void setHistory(History history){
      this.history= history;
    }

    public void someMethod(){
      history.addToHistoryArea();
    }

}

Or if you don't want to have an instance in Server

public void someMethod(History history){
      history.addToHistoryArea();
}

Or if you want to be more decoupled you can take approach with the observer pattern or perhaps a mediator if they are colleagues.

You may want to create a History object in the Server class and then call the addToHistoryArea() method on that history instance.

public class Server{

    private History history;

    public void setHistory(History history){
        this.history = history;
    }

    public void methodCall(){
        history.addToHistoryArea();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM