Java 1.5 引入了Closeable单一方法接口close()。在 Java 1.7 中添加了一个运算符,try-with-resources并为此引入了一个接口,该接口AutoCloseable成为父接口Closeable。
据我了解,这样做是为了让所有实现该接口的类Closeable都可以自动在try-with-resources. 但是后来进入这个界面的意思就一点都不清楚了。为什么不让这个操作员立即使用现有界面Closeable,而不是新界面AutoCloseable呢?
还是这样做只是为了让该方法close()可以抛出任何异常,而不仅仅是来自 的孩子IOException?
close我们希望通过引入 try-with-resources 结构来放宽对接口方法Closeable的两个限制:IOException不适合所有人。例如,对于java.sql.Connection。AutoCloseable没有,尽管它是“强烈推荐的”。