我正在编写一个将模拟Java中的Kerberos协议的代码。我有一个服务器类和一个客户端类。但有些内容是静态的,其他内容不是,加上套接字,我混在一起说至少。我认为协议的细节在这个问题上是任意的。从子线程访问父实例
我有一个服务器类,它调用ServerThread类:
public class Server{
public void someMethod(){ /* some code */ }
public static void main(String args[]){
ServerSocket serverSocket = new ServerSocket(port);
new ServerThread(serverSocket.accept()).start();
}
}
public class ServerThread extends Thread{
/* constructor (takes serverSocket from Server) */
this.parent.someMethod();
/* That would call someMethod() from the parent class Server instance
* that instantiated this.
*/
}
来实例化ServerThread是给我的服务器类的一部分,我不得不使用它原样。我自己编写的someMethod()方法,这是我想从ServerThread中使用的方法。有没有办法做到这一点,称为this.parent.someMethod()的代码行?如果可以,是否有办法从单个控制器类访问这两个类,或者静态内容与非静态内容是否会破坏该想法?
何时分配了“服务器”实例? – 2012-04-20 15:40:46
对不起,服务器在服务器的主方法中实例化,就在ServerSocket之前。这可以移动。任何此代码都可以移动。如果需要,主要方法可以在单独的类中。 – rshaq 2012-04-20 15:43:21