在java中添加AutoCloseable是否重要?
在java中实现自动关闭是否重要 如果我创建一个实现AutoCloseable的类来扩展另一个没有实现它的类,这有意义吗 e、 g AutoCloseable是一个接口,它本质上允许在try with resources语句中使用对象时自动关闭对象的资源。如果您不打算将对象与try-with资源一起使用,则根本不需要实现它 作为继承的一条规则,不,做你想做的事本身并没有错。如果要自动关闭子类中的资源,请继续。AutoCloseable是一个接口,它本质上允许在try with resources语句中使用对象的资源时自动关闭该对象的资源。如果您不打算将对象与try-with资源一起使用,则根本不需要实现它在java中添加AutoCloseable是否重要?,java,autocloseable,Java,Autocloseable,在java中实现自动关闭是否重要 如果我创建一个实现AutoCloseable的类来扩展另一个没有实现它的类,这有意义吗 e、 g AutoCloseable是一个接口,它本质上允许在try with resources语句中使用对象时自动关闭对象的资源。如果您不打算将对象与try-with资源一起使用,则根本不需要实现它 作为继承的一条规则,不,做你想做的事本身并没有错。如果要自动关闭子类中的资源,请继续。AutoCloseable是一个接口,它本质上允许在try with resources
作为继承的一条规则,不,做你想做的事本身并没有错。如果您想自动关闭子类中的资源,请继续。让一个类实现AutoClosable没有问题,即使它的超级类没有。实际上,由于Java中的所有类都直接或间接地扩展了Java.lang.Object,因此所有自动可关闭项最终都扩展了一个不实现此接口的类
如果您的类具有关闭它的语义,那么您可能应该让它实现AutoClosable。这样做几乎不需要花费任何成本,而且它允许您使用Java 7简洁的try with resource语法的语法糖化。让一个类实现AutoClosable没有问题,即使它的超级类没有。实际上,由于Java中的所有类都直接或间接地扩展了Java.lang.Object,因此所有自动可关闭项最终都扩展了一个不实现此接口的类
如果您的类具有关闭它的语义,那么您可能应该让它实现AutoClosable。这样做几乎不需要花费任何成本,而且它允许您使用Java 7对try with resource语法的简洁语法糖化。来自文档: 无效关闭 抛出异常 关闭此资源,放弃任何基础资源。此方法在try with resources语句管理的对象上自动调用 虽然此接口方法声明为抛出异常,但强烈鼓励实现者声明close方法的具体实现以抛出更具体的异常,或者如果close操作不能失败,则根本不抛出异常
因此,如果您希望在try-with-resources语句中自动关闭新类,则可以将该功能添加到您的ICannotBeClosed类中 来自文档: 无效关闭 抛出异常 关闭此资源,放弃任何基础资源。此方法在try with resources语句管理的对象上自动调用 虽然此接口方法声明为抛出异常,但强烈鼓励实现者声明close方法的具体实现以抛出更具体的异常,或者如果close操作不能失败,则根本不抛出异常 因此,如果您希望在try-with-resources语句中自动关闭新类,则可以将该功能添加到您的ICannotBeClosed类中 在java中实现自动关闭是否重要 很难说实现接口是否重要。但这不是必须的 如果我创建一个实现AutoCloseable的类,这有意义吗 扩展另一个未实现它的类 这样做没关系。没问题 自动关闭是从Java 7添加的。它设计用于新的try with resources语句java7+ 请参阅下面提供相同功能的两个类。一个不使用自动关闭,另一个使用自动关闭:
// Not use AutoClosable
public class CloseableImpl {
public void doSomething() throws Exception { // ... }
public void close() throws Exception { // ...}
public static void main(String[] args) {
CloseableImpl impl = new CloseableImpl();
try {
impl.doSomething();
} catch (Exception e) {
// ex from doSomething
} finally {
try { // impl.close() must be called explicitly
impl.close();
} catch (Exception e) {
}
}
}
}
// Use AutoCloseable
public class AutoCloseableImpl implements AutoCloseable {
public void doSomething() throws Exception { // ... }
public void close() throws Exception { // ...}
public static void main(String[] args) {
// impl.close() will be called implicitly
try (AutoCloseableImpl impl = new AutoCloseableImpl()) {
impl.doSomething();
} catch (Exception e) {
// ex from doSomething
}
}
}
如你所见。使用自动关闭将使代码更短、更清晰
在java中实现自动关闭是否重要
很难说实现接口是否重要。但这不是必须的
如果我创建一个实现AutoCloseable的类,这有意义吗
扩展另一个未实现它的类
这样做没关系。没问题
自动关闭是从Java 7添加的。它设计用于新的try with resources语句java7+
请参阅下面提供相同功能的两个类。一个不使用自动关闭,另一个使用自动关闭:
// Not use AutoClosable
public class CloseableImpl {
public void doSomething() throws Exception { // ... }
public void close() throws Exception { // ...}
public static void main(String[] args) {
CloseableImpl impl = new CloseableImpl();
try {
impl.doSomething();
} catch (Exception e) {
// ex from doSomething
} finally {
try { // impl.close() must be called explicitly
impl.close();
} catch (Exception e) {
}
}
}
}
// Use AutoCloseable
public class AutoCloseableImpl implements AutoCloseable {
public void doSomething() throws Exception { // ... }
public void close() throws Exception { // ...}
public static void main(String[] args) {
// impl.close() will be called implicitly
try (AutoCloseableImpl impl = new AutoCloseableImpl()) {
impl.doSomething();
} catch (Exception e) {
// ex from doSomething
}
}
}
如你所见。使用自动关闭将使代码更短、更干净。这基本上不是问题。您对该代码的具体使用是什么?我能想到的最大好处是避免对我们造成伤害的可能性
在try-catch中创建finally块,使用仅适用于自动关闭的try-with-resources。因此,如果您创建了一个具有关闭方法的类,我将实现AutoCloseable,即使超类无法关闭。由于Closeable扩展了AutoCloseable,因此选择AutoCloseable而不是Closeable的唯一原因是close方法需要抛出除IOException之外的已检查异常。这基本上不是问题。这段代码的具体用途是什么?我所能想到的最大好处是,可以避免在try-catch中使用finally块,使用try-with-resources,而try-with-resources只适用于AutoCloseables。因此,如果您创建了一个具有关闭方法的类,即使超类无法关闭,我也会实现AutoCloseable。由于Closeable扩展了AutoCloseable,因此选择AutoCloseable而不是Closeable的唯一原因是close方法需要抛出除IOException之外的已检查异常。