黑马程序员技术交流社区
标题:
关于IO这方面的问题:!!!!!!!
[打印本页]
作者:
黑马张旭
时间:
2013-1-28 18:00
标题:
关于IO这方面的问题:!!!!!!!
class Averagecount extends Thread{
private DataInputStream Din;
double total=0;
long count=0;
public Averagecount(InputStream consumer){
Din=new DataInputStream(consumer);
}
public void run(){
while(true){
double num;
try {
num = Din.readDouble();
total+=num;
count++;
System.out.println("The average is "+ total/count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Randomnumber extends Thread{
private DataOutputStream outs;
private Random ran=new Random();
private final long Term= 1000;
public Randomnumber(OutputStream out){
outs=new DataOutputStream(out);
}
public void run(){
while(true){
double num=ran.nextDouble()*Term;
try {
outs.writeDouble(num);
outs.flush();
sleep(500);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
}
}
}
}
public class FileTest{
public static void main(String[] args){
try {
PipedInputStream producter=new PipedInputStream();
PipedOutputStream consumer=new PipedOutputStream(producter);
Averagecount avc=new Averagecount(consumer);
Randomnumber ranu=new Randomnumber(producter);
avc.start();
ranu.start();
try {
Thread.sleep(10000);
} catch (Exception e) {
// TODO: handle exception
}
avc.stop();
ranu.stop();
producter.close();
consumer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
大神求指教。为什么这两个:
Averagecount avc=new Averagecount(consumer);
Randomnumber ranu=new Randomnumber(producter);
无法进行实例化。pipe是inputstream的子类,然后filter是inputstream的子类,data是filter的子类。然后是不是data的构造器只能够接受input类和他的父类,不能够是其他input的子类么?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2