gpt4 book ai didi

java - 为什么Java7专门引入了AutoCloseable?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:16:10 29 4
gpt4 key购买 nike

<分区>

AutoCloseable在jdk1.7中引入,Cloesable在jdk1.5中已经存在。

并根据https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

因此,Closeable 实例已经可以在 try-with-resources 语句中被视为资源。这是肯定的,因为 CloseableAutoCloseable 扩展而来。

我的问题是为什么java专门引入了AutoCloseable,为什么不让Closeable只在try-with-resources中支持,有没有其他方法除了 try-with-resources 之外,是否可以使用 AutoCloseable?

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com