关于静态属性无法成功更改的问题
代码如下:----------------------------------------------------------------------------
import *;
import *;
import java.util.*;
class A {
public ServerSocket server = null;
public Socket client = null;
public static boolean flag = true; //此处我设置成静态
public Scanner sc = null;
public A() throws Exception {
server = new ServerSocket(8888);
System.out.println("已建立服务端");
while (A.flag) { //如果A.flag为false则,服务端终止
client = server.accept();
System.out.println("一客户端连入");
sc = new Scanner(client.getInputStream());
while (sc.hasNext()) {
String str = sc.nextLine();
System.out.println(str);
}
sc.close();
}
}
}
class B {
public Socket client = null;
public Scanner sc = new Scanner(System.in);
public PrintStream ps = null;
public boolean flag = true;
public B() throws Exception {
client = new Socket("localhost", 8888);
ps = new PrintStream(client.getOutputStream());
System.out.println("客户端连入端口号为8888的服务端");
while (flag) {
String str = sc.nextLine();
if ("bye".equals(str)) {
ps.println(str);
A.flag = false; //结果这里并没有成功更改A类的静态属性,为何?
flag = false;
} else {
ps.println(str);
}
}
ps.close();
client.close();
}
}
public class Test24 {
public static void main(String[] args) throws Exception {
//new A();//先运行A
//new B();//在运行B
}
}