A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 8161776 中级黑马   /  2012-5-23 15:27  /  1511 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨尧 于 2012-5-23 15:29 编辑
  1. public class TCPServer {
  2.         public static void main(String[] args){
  3.                 System.out.println("服务端已启动");
  4.                 ServerSocket serverSocket = null;
  5.                 try {
  6.                         serverSocket = new ServerSocket(7878);
  7.                         Socket clentSocket = serverSocket.accept();
  8.                         new Thread(new ClentRun(clentSocket));//这一行为什么会编译错误呢?纠结死我了
  9.                 } catch (IOException e) {
  10.                         e.printStackTrace();
  11.                 } finally {
  12.                         if(serverSocket != null){
  13.                                 try {
  14.                                         serverSocket.close();
  15.                                 } catch (IOException e) {
  16.                                         e.printStackTrace();
  17.                                 }
  18.                         }
  19.                                 
  20.                 }
  21.                
  22.         }

  23.         public class ClentRun implements Runnable{
  24.                 Socket clent = null;
  25.                 public ClentRun(Socket clent){
  26.                         this.clent = clent;
  27.                 }
  28.                
  29.                 public void run() {
  30.                         System.out.println(clent.getInetAddress().getHostAddress()+"连接成功");
  31.                         try {
  32.                                 BufferedInputStream bis=new BufferedInputStream(clent.getInputStream());
  33.                                 BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D://2.txt"));
  34.                                 PrintWriter pw=new PrintWriter(clent.getOutputStream(),true);
  35.                                 int len=0;
  36.                                 boolean flag = true;
  37.                                 while(flag){
  38.                                         System.out.println((char)len);
  39.                                         bos.write(len);
  40.                                         bos.flush();
  41.                                 }
  42.                                 pw.println("上传成功");
  43.                         } catch (IOException e) {
  44.                                 e.printStackTrace();
  45.                         }
  46.                 }
  47.         }
  48. }
复制代码
第8行为什么会编译错误呢?
这个程序可以不看别的地方,下面是我定义的一个内部类,是线程,在上面为什么不能启动呢?我还纠结,高手帮忙看看



评分

参与人数 1技术分 +1 收起 理由
攻城狮 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
本帖最后由 刘克方 于 2012-5-23 16:11 编辑

new Thread(new ClentRun(clentSocket));
将这一句代码改为 new Thread(new TCPServer().new ClentRun(clentSocket));


评分

参与人数 1技术分 +1 收起 理由
攻城狮 + 1 赞一个!

查看全部评分

回复 使用道具 举报
就是因为楼主定义的内部类的关系。关于外部类怎样引用内部类要看内部类是静态的或非静态的:
非静态的:
new Outer().new Inter()
静态的:
Outer.Inter
内部类在基础视频中也有多次强调,而且群里也有相关主题,楼主可以找来看一下

评分

参与人数 1技术分 +1 收起 理由
攻城狮 + 1 赞一个!

查看全部评分

回复 使用道具 举报
public class TCPServer {
        public static void main(String[] args){
                System.out.println("服务端已启动");
                ServerSocket serverSocket = null;
                try {
                        serverSocket = new ServerSocket(7878);
                        Socket clentSocket = serverSocket.accept();
                        new Thread(new ClentRun(clentSocket)).start();;//这一行为什么会编译错误呢?纠结死我了
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if(serverSocket != null){
                                try {
                                        serverSocket.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                                
                }
               
        }

        public class ClentRun implements Runnable{
                Socket clent = null;
                public ClentRun(Socket clent){
                        this.clent = clent;
                }
               
                public void run() {
                        System.out.println(clent.getInetAddress().getHostAddress()+"连接成功");
                        try {
                                BufferedInputStream bis=new BufferedInputStream(clent.getInputStream());
                                BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D://2.txt"));
                                PrintWriter pw=new PrintWriter(clent.getOutputStream(),true);
                                int len=0;
                                boolean flag = true;
                                while(flag){
                                        System.out.println((char)len);
                                        bos.write(len);
                                        bos.flush();
                                }
                                pw.println("上传成功");
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马