读书人

一个类中的私有成员变量这个类中的方

发布时间: 2012-04-18 15:01:59 作者: rapoo

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

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();
点里面的方法

读书人网 >J2SE开发

热点推荐