日期:2014-05-20  浏览次数:20765 次

一个类中的私有成员变量,这个类中的方法就不能直接访问么?
Java code

class Client implements Runnable {
        private Socket s;
        private DataInputStream dis = null;
        private DataOutputStream dos = null;
        private boolean bConnected = false;
        
        public Client(Socket s) {
            this.s = s;
            try {
                dis = new DataInputStream(s.getInputStream());
                dos = new DataOutputStream(s.getOutputStream());
                bConnected = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public void send(String str) {
            try {
                dos.writeUTF(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

                public void run() { .....}
}






因为Socket s 是私有的,那么 dos 使用 就不能直接使用 s.getInputStream() ,那么,为什么要构造一个send方法,作为  
  dos 的 写出方法呢?

为什么不能直接使用dos.writrUTF();

------解决方案--------------------
Socket dd =??
Client c = new Client(dd);
c.send("s");
c.run();
点里面的方法