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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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的子类么?

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马