当前位置: 代码迷 >> J2SE >> 一个类中的私有成员变量,这个类中的方法就不能直接访问么?解决办法
  详细解决方案

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

热度:117   发布时间:2016-04-24 01:21:07.0
一个类中的私有成员变量,这个类中的方法就不能直接访问么?
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();
点里面的方法
  相关解决方案