简体   繁体   English

在Java中添加AutoCloseable是否重要?

[英]Is it important to add AutoCloseable in java?

Is it important to implement AutoCloseable in java? 在Java中实现AutoCloseable重要吗?

Would it make sense if I create a class which implements AutoCloseable extends a other class which doesn't implement it? 如果我创建一个实现AutoCloseable的类扩展了另一个未实现该类的类,这会有意义吗?

eg 例如

public class IGetClosed extends ICannotBeClosed implements AutoCloseable {...}

Is it important to implement autoCloseable in java? 在Java中实现autoCloseable重要吗?

It is hard to tell it is important or not to implement the interface. 很难说实现该接口是否重要。 But it is not required. 但这不是必需的。

Would it make sense if I create a class which implement AutoCloseable extends a other class which doesn't implement it? 如果我创建一个实现AutoCloseable的类扩展了另一个未实现该类的类,这会有意义吗?

It is OK to do that. 可以这样做。 Nothing wrong. 没有什么不对。

AutoCloseable was added from Java 7. It is designed to use with new try-with-resources statement (Java 7+) AutoCloseable是Java 7的新增功能。它旨在与新的try-with-resources语句一起使用(Java 7+)

See two below classes providing the same functionality. 请参见下面两个提供相同功能的类。 One is not using AutoCloseable and the other is using AutoClosable: 一个不使用AutoCloseable,另一个不使用AutoClosable:

// 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
        }
    }
}

As you see. 正如你看到的。 Using AutoClosble will make the code shorter and cleaner. 使用AutoClosble将使代码更短,更清晰。

AutoCloseable is an interface that essentially allows an object's resources to be closed automatically when using it in a try-with-resources statement. AutoCloseable是一个接口,本质上允许在try-with-resources语句中使用对象时自动关闭对象的资源。 If you don't plan on using your object with try-with-resources, there is no need to implement it at all. 如果您不打算将对象与try-with-resources一起使用,则根本不需要实现它。

As a rule of inheritance, no, there is nothing inherently wrong with doing what you want to do. 作为继承的规则,不,做您想做的事情本质上没有错。 If you want to auto-close resources in your child class, go right ahead. 如果要自动关闭子类中的资源,请继续。

There's no problem in having a class implement AutoClosable even if it's super class doesn't. 让一个类实现AutoClosable是没有问题的,即使它不是超类也没有。 Actually, as all the classes in Java extend java.lang.Object either directly or indirectly, all the AutoClosable s eventually extend a class that doesn't implement this interface. 实际上,由于Java中的所有类都直接或间接扩展了java.lang.Object ,因此所有AutoClosable最终都会扩展一个不实现此接口的类。

If your class has some semantic of close ing it, you should probably have it implement AutoClosable . 如果您的类具有close它的某种语义,则可能应该使它实现AutoClosable It costs almost nothing to do so, if at all, and it allows you to use Java 7's neat syntactic sugaring of the try-with-resource syntax. 这样做几乎不需要任何成本,并且它允许您使用Java 7的try-with-resource语法的巧妙语法。

From the documentation: 从文档中:

void close() throws Exception void close()引发异常

Closes this resource, relinquishing any underlying resources. 关闭此资源,放弃所有基础资源。 This method is invoked automatically on objects managed by the try-with-resources statement. 在try-with-resources语句管理的对象上自动调用此方法。

While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail. 尽管声明此接口方法引发Exception,但强烈建议实现人员声明close方法的具体实现以引发更具体的异常,或者如果close操作不会失败,则根本不引发任何异常。

So if you want the new class to be automagically closed in a try-with-resources statement, you could thus add that functionality to your ICannotBeClosed class. 因此,如果希望在try-with-resources语句中自动关闭新类,则可以将该功能添加到ICannotBeClosed类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM