在毕老师讲解管道流的21天视频中定义的内部类Write和Read类都是非静态的 那为什么在Demo类中静态的main方法中还能调用Write和Read类来实例化对象,反正我像那样使用内部类来定义Write和Read类是不能再Demo类中静态的main方法中调用,只有将他们改成static的才能使用,代码如下,有人知道原因吗?package com.hubj.objectDemo;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
}
static class Write implements Runnable {
PipedOutputStream out = new PipedOutputStream();
public Write(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
out.write("piped stream is coming ".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class Read implements Runnable {
PipedInputStream in = new PipedInputStream();
public Read(PipedInputStream in) {
this.in = in;
}
@Override
public void run() {
byte[] b = new byte[1024];
try {
int len = in.read(b);
String s = new String(b, 0, len);
System.out.println(s);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
|
|