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

java 测试Piped* 报错,求大神指点,谢谢!
[code=Java][/code]
package com.pipedstream;

import java.io.*;

public class PipedStreamTest {

 /**

  * @param args

  */

 public static void main(String[] args){

  // TODO Auto-generated method stub

  Thread t1 = new Sender();

  Thread t2 = new Receiver();

  PipedOutputStream os=t1.getOutput(); //The method getOutput() is undefined for the type Thread

  PipedInputStream is=t2.getInput(); //The method getInput() is undefined for the type Thread

  is.connect(os);

  t1.start();

  t2.start();

 }

}


class Sender extends Thread {
 private PipedOutputStream out = new PipedOutputStream();

 public PipedOutputStream getOutput(){

  return out;

  }

  public void run(){

  try{

  out.write("hello".getBytes());

  out.close();

  }catch(IOException e){

  e.printStackTrace();

  }
 

  }
}


class Receiver extends Thread {

 

 private PipedInputStream in = new PipedInputStream();

 public PipedInputStream getInput(){

  return in;

  }

  public void run(){

  byte[] buf = new byte[1024];

  try{

  int len = in.read(buf);

  System.out.println("receive from sender:\n"+new String(buf,0,len));

  in.close();

  }catch(IOException e){

  e.printStackTrace();

  }

   

  }

  
}
问:大家帮忙看一下main()函数提示的两个错误,找不到问题···


------解决方案--------------------
Java code

import java.io.*;

public class PipedStreamTest {

    /**
     *
     * @param args
     *
     */
    public static void main(String[] args) {
// TODO Auto-generated method stub
        Sender t1 = new Sender();
        Receiver t2 = new Receiver();
        PipedOutputStream os = t1.getOutput(); //The method getOutput() is undefined for the type Thread
        PipedInputStream is = t2.getInput(); //The method getInput() is undefined for the type Thread
        try {
            is.connect(os);
        } catch (IOException e) {
            e.printStackTrace();
        }

        t1.start();

        t2.start();
    }
}

class Sender extends Thread {

    private PipedOutputStream out = new PipedOutputStream();

    public PipedOutputStream getOutput() {
        return out;
    }

    public void run() {
        try {
            out.write("hello".getBytes());
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Receiver extends Thread {

    private PipedInputStream in = new PipedInputStream();

    public PipedInputStream getInput() {
        return in;
    }

    public void run() {
        byte[] buf = new byte[1024];
        try {
            int len = in.read(buf);
            System.out.println("receive from sender:\n" + new String(buf, 0, len));
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

------解决方案--------------------
你定义的类型不对