Java尝试使用资源和“自动固有”接口

发布于 2025-02-08 12:25:06 字数 321 浏览 0 评论 0原文

我正在寻找带有语句的Python的Java等效物,并且我阅读了有关实现AutoCoclosable接口的信息,并将其与Resources一起使用。

在Python中,上下文管理器(语句)使用两种方法:__输入____ exit __,但是在Java中,使用Resources Block的尝试仅使用关闭,等效于__退出__

__输入__方法是否有等效词,以便在使用资源块输入尝试时自动执行某个方法,而不仅仅是块结束时?

I am looking for a Java equivalent for python's with statement, and I read about implementing the AutoCloseable interface and using try with resources.

In python, the context manager (with statement) uses two methods: __enter__ and __exit__, but in Java, the try with resources block uses only close, which is the equivalent of __exit__.

Is there an equivalent for the __enter__ method, in order to perform a certain method automatically when entering the try with resources block, and not only when the block is over?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女净妖师 2025-02-15 12:25:06

等效的基本上是您在尝试中呼叫的任何内容,以获取您的AutoCoclosable的实例。这可能是一个类似的构造函数:

try (MyClass obj = new MyClass()) { …

具有这样的构造函数的班级看起来像:

public class MyClass implements AutoCloseable {
    public MyClass() {
        // do "enter" things...
    }

    @Override
    public void close() {
        // close resources
    }
}

取决于您需要的“输入”要做的事情,您可能更喜欢班级的静态生产商,这看起来像这样:

try (MyClass obj = MyClass.getInstance(someProperties)) { …

那么您的班级可能会看起来像类似的内容:

public class MyClass implements AutoCloseable {
    private MyClass() {
        // instantiate members
    }

    public static MyClass getInstance(Properties config) {
        // you could implement a singleton pattern or something instead, for example
        MyClass obj = new MyClass();
        // read properties...
        // do "enter" things...
        return obj;
    }

    @Override
    public void close() {
        // close resources
    }
}

您甚至可以在尝试中调用工厂或建筑商模式来产生您的Autococlosable。这完全取决于您的设计以及您需要在“ Enter”上执行的实例。

The equivalent is basically whatever you are calling in the try to get an instance of your AutoCloseable. This could be a constructor like:

try (MyClass obj = new MyClass()) { …

Where the class having such a constructor looks like:

public class MyClass implements AutoCloseable {
    public MyClass() {
        // do "enter" things...
    }

    @Override
    public void close() {
        // close resources
    }
}

Depending on what you need "enter" to do, you might instead prefer a static producer for your class, which would look like this:

try (MyClass obj = MyClass.getInstance(someProperties)) { …

Then your class might look something like this:

public class MyClass implements AutoCloseable {
    private MyClass() {
        // instantiate members
    }

    public static MyClass getInstance(Properties config) {
        // you could implement a singleton pattern or something instead, for example
        MyClass obj = new MyClass();
        // read properties...
        // do "enter" things...
        return obj;
    }

    @Override
    public void close() {
        // close resources
    }
}

You could even call a factory or builder pattern in the try to produce your AutoCloseable. It all depends on your design and what you need the instance to do on "enter".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文