jdk中的AutoCloseable接口使用
举个例子:
public class AutoClient implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("I'm closed.");
}
}
测试:
public class AutoClientTest {
public static void main(String[] args) {
try (AutoClient client = new AutoClient()){
System.out.println("I'm doing something.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果:
I'm doing something.
I'm closed.
AutoCloseable
的close方法需要和try(){}
一起使用