AutoCloseable接口: 通过一个例子 举例自动关闭流的实现。
public interface BaseStream<T, S extends BaseStream<T, S>> extends AutoCloseable{} // BaseStream 继承了这个接口。 Stream继承了Stream public class AutoCloseableTest implements AutoCloseable { public void dosomething() { System.out.println(" do something "); } @Override public void close() throws Exception { System.out.println(" close invoked "); } public static void main(String[] args) throws Exception { try ( AutoCloseableTest autoCloseableTest = new AutoCloseableTest()){ autoCloseableTest.dosomething(); } } }运行结果如下: 自动调用了关闭流的方法