谁能启发我?我试图访问属于父类(主小程序类)的对象中的套接字。我希望我能为你做这个更简洁。有点拗口,但也许我的消息来源会进一步解释。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
class SocketsC {
Socket clientsocket;
static PrintWriter out;
static BufferedReader in;
public void SocketsC() {
}
public void connectSock() {
try {
clientsocket = new Socket( "localhost", 9900 );
out = new PrintWriter(clientsocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader( clientsocket.getInputStream()));
} catch (UnknownHostException e) {
System.exit(1);
} catch (IOException e) {
System.exit(1);
}
}
}
class AccessSock {
AccessSock() {
// Here is where I am trying to access the socket from the mysocks object
mysocks.out.println( "working" ); // This doesn't work...
}
}
public class main extends JApplet {
static SocketsC mysocks;
public void init() {
createApp();
}
public void createApp() {
SocketsC mysocks = new SocketsC();
AccessSock sendtext = new AccessSock();
}
}
TYIA-罗兰