在方法中看到过这个评论:
//I wonder why Sun made input and output streams implement Closeable and left Socket behind
Run Code Online (Sandbox Code Playgroud)
它会阻止创建包装器匿名内部类,它实现了Closeable,它将close方法委托给Socket实例.
在Java5中引入了Closeable,而在JDK 1.0中引入了Socket.在Java7中,Socket将是可关闭的.
编辑
只需通过测试close方法的存在,就可以使用反射来关闭Java 4/5/6中的任何"可关闭"对象.使用此技术允许您关闭,例如,ResultSet(具有close()方法但不实现Closeable):
public static universalClose(Object o) {
try {
o.getClass().getMethod("close", null).invoke(o, null);
} catch (Exception e) {
throw new IllegalArgumentException("missing close() method");
}
}
Run Code Online (Sandbox Code Playgroud)