java从另一个类调用方法

java从另一个类调用方法,java,class,methods,constructor,Java,Class,Methods,Constructor,免责声明-这是一个任务,我试图找出为什么我的代码给我一个错误 背景: 因此,任务基本上是对SimpleChat进行编辑,这是一个客户机-服务器框架,老师为我们提供了代码。我试图从服务器端实现一个聊天控制台,并实现以hashtags开始的操作。因此,在我的服务器文件(称为“EchoServer”)中有一个“handleMessageFromServer”方法,我试图从服务器控制台(恰当地命名为“服务器控制台”)调用它。我将把相关代码放在下面,并加以解释: 我会编辑我放的东西,我会把所有的文件都放上

免责声明-这是一个任务,我试图找出为什么我的代码给我一个错误

背景:

因此,任务基本上是对SimpleChat进行编辑,这是一个客户机-服务器框架,老师为我们提供了代码。我试图从服务器端实现一个聊天控制台,并实现以hashtags开始的操作。因此,在我的服务器文件(称为“EchoServer”)中有一个“handleMessageFromServer”方法,我试图从服务器控制台(恰当地命名为“服务器控制台”)调用它。我将把相关代码放在下面,并加以解释:

我会编辑我放的东西,我会把所有的文件都放上注释

下面是“EchoServer”文件。与我的问题相关的更重要的部分是“handleMessageFromServer”方法,我试图从另一个文件调用该方法并传入信息,以及EchoServer构造函数,我在另一个文件中使用该构造函数创建实例

import java.io.*;
import ocsf.server.*;
import common.*;

/**
 * This class overrides some of the methods in the abstract 
 * superclass in order to give more functionality to the server.
 *
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Laganière
 * @author François Bélanger
 * @author Paul Holden
 * @version July 2000
 */
public class EchoServer extends AbstractServer 
{
  //Class variables *************************************************

  /**
   * The default port to listen on.
   */
  final public static int DEFAULT_PORT = 5555;

  //Constructors ****************************************************

  /*
   * An interface type variable, will allow the implementation of the 
   * Display method in the client.
   * 
   */
  public ChatIF server;
  /**
   * Constructs an instance of the echo server.
   *
   * @param port The port number to connect on.
   */
  public EchoServer(int port, ChatIF server)
  {
      super(port);
      this.server = server;
  }

  //Instance methods ************************************************

  /**
   * This method handles any messages received from the client.
   *
   * @param msg The message received from the client.
   * @param client The connection from which the message originated.
   */
  public void handleMessageFromClient
    (Object msg, ConnectionToClient client)
  {
    System.out.println("Message received: " + msg + " from " + client);
    this.sendToAllClients(msg);
  }

  /**
   * This method handles any messages received from the server console.
   *
   * @param msg The message received from the server.
   * @param server The connection from which the message originated.
   */
  public void handleMessageFromServer(String message)
  {
    if (message.charAt(0) == '#')
    {
      serverCommand(message);
    }
    else
    {
      server.display(message);
      this.sendToAllClients("SERVER MSG> " + message);
    }
  }
  /*This method allows us to run commands from the server console
   * 
   */
  public void serverCommand(String command)
  {
      if (command.equalsIgnoreCase("quit")) System.exit(0);      /////Shuts the system down
      else if (command.equalsIgnoreCase("#stop")) stopListening();  ///Stops listening for connections
      else if (command.equalsIgnoreCase("#close"))            ///////closes all connections
      {
          try close();
                  catch(IOException ex) {
                      server.display("Could not close connection");
                  }
      }
      else if (command.toLowerCase().startsWith("#setport"))           /////Sets port when not listening
      {
        if (!isListening() && getNumberOfClients() == 0)
        {
            //////If there are no connected clients, and
            //////we're not listening for new ones, we can 
            ////assume that the server is closed (close() has been 
            ////called. 
            String portNum = command.substring(s.indexOf("<") + 1)
            portNum = portnum.substring(0, s.indexOf(">"));
            int num = Integer.parseInt(portNum);
            setPort(num);
            server.display("The server port has been changed to port" + getPort());
        }
        else
        {
            server.display("Port cannot be changed");
        }
        else if (command.equalsIgnoreCase("#start"))     ///////starts listening for clients if not already
        {
            if (!isListening())
            {
                try listen();
                catch (Exception ex)
                {
                    server.display("Could not listen for clients!");
                }
            }
            else
            {
                server.display("Already listening for clients");
            }
        }
        else if (message.equalsIgnoreCase("#getport"))    //////gets the port number
        {
            server.display("Current port: " + Integer.toString(getPort()));
        }
      }
  }

  /**
   * This method overrides the one in the superclass.  Called
   * when the server starts listening for connections.
   */
  protected void serverStarted()
  {
    System.out.println
      ("Server listening for connections on port " + getPort());
  }

  /**
   * This method overrides the one in the superclass.  Called
   * when the server stops listening for connections.
   */
  protected void serverStopped()
  {
    System.out.println
      ("Server has stopped listening for connections.");
  }

  //Class methods ***************************************************

  /**
   * This method is responsible for the creation of 
   * the server instance (there is no UI in this phase).
   *
   * @param args[0] The port number to listen on.  Defaults to 5555 
   *          if no argument is entered.
   */
  public static void main(String[] args) 
  {
    int port = 0; //Port to listen on

    try
    {
      port = Integer.parseInt(args[0]); //Get port from command line
    }
    catch(Throwable t)
    {
      port = DEFAULT_PORT; //Set port to 5555
    }

    EchoServer sv = new EchoServer(port);

    try 
    {
      sv.listen(); //Start listening for connections
    } 
    catch (Exception ex) 
    {
      System.out.println("ERROR - Could not listen for clients!");
    }
  }
}
//End of EchoServer class

我想我可能没有正确地构造实例,但是非常感谢各位的帮助

echoserver类具有以下构造函数:

public EchoServer(int port, ChatIF server)
  {
      super(port);
      this.server = server;
  }
注意,它包含两个参数

但是,对EchoServer的调用只是注入一个端口

this.echoServer = new EchoServer(port);

如果没有看到您所有的代码,我猜Echoserver会扩展其他一些没有您想要的方法的服务器类

错误消息是什么?与其把它们分成碎片,不如编辑帖子,将方法放入各自的类中,并在需要的地方添加注释检查字段的范围是什么。这是try-catch块中的一个错误,因为您声明了两个不同的
host
。这将有助于查看上一个框的两个部分都在哪里。与此问题相关的是方法的定义位置、签名以及调用它的位置和方式。实际上,它正在扩展另一个服务器文件(参考名为“AbstractServer”)。但是,我将该方法放在EchoServer文件中,并且没有其他继承自AbstractServer文件的子类,我是否应该不能通过EchoServer调用它?
this.echoServer = new EchoServer(port);