1
我想了解Java的抽象类,所以我在的IntelliJ写了这个代码片段:为什么我可以实例化这个抽象类?
AutoCloseable ac = new BufferedInputStream(new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
});
的@Override
和read()
存根被的IntelliJ自动创建。
由于InputStream
是一个抽象类,为什么我可以用new
关键字实例化它?
而另一件事。当我删除方法存根这样的:
AutoCloseable ac = new BufferedInputStream(new InputStream());
的IDE说InputStream
是抽象的,因此不能被实例化(如预期)。
那么,为什么前者是有效的而后者不是?
这个read()
方法从哪里来?