奇妙巧妙关闭流 AutoCloseable

AutoCloseable接口

接口功能

全称java.lang.AutoCloseable,jdk1.7引入、官方文档说明:

一个可以保存资源(如文件或套接字句柄)直到它被关闭的对象。AutoCloseable对象的close()方法在退出资源块(try-with-resources block)时被自动调用,资源块是在资源规范头中声明的对象。这种构造确保了快速释放,避免了可能发生的资源耗尽异常和错误。

 

package com.wxrem.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author wxb
 * 奇妙巧妙关闭流 AutoCloseable
 * @date 2020-09-03 15:28
 */
public class AutoCloseableDemo {
    public static void main(String[] args) {
        try (AutoCloseableObjecct app = new AutoCloseableObjecct()) {
            System.out.println("--执行main方法--");
            System.out.println("--demo2--");
            app.demo2();
        } catch (Exception e) {
            System.out.println("--exception--");
        } finally {
            System.out.println("--finally--");
        }
//结果:
// --执行main方法--
//--demo2--
//--fileInputStream2--
//--close fileInputStream2--
//--finally--
    }

    //自己定义类 并实现AutoCloseable
    public static class AutoCloseableObjecct implements AutoCloseable {
        //其中close()方法是关闭流并且释放与其相关的任何方法,如果流已被关闭,那么调用此方法没有效果。
        @Override
        public void close() throws Exception {
            System.out.println("--close fileInputStream2--");
        }

        public static void demo2() {

            //JDK1.7之前,释放资源方式
//        FileInputStream fileInputStream = null;
//        try {
//            fileInputStream = new FileInputStream("");
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                fileInputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }

            //1.7之后,只要实现了AutoCloseable接口
            try (FileInputStream fileInputStream2 = new FileInputStream("F:\\server (2).log")) {
                System.out.println("--fileInputStream2--");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,创建一个奇妙农场的入库系统,我们可以设计一个类来模拟仓库和库存操作。以下是一个简单的示例,展示了如何使用类、对象和方法来管理农场产品入库: ```cpp #include <iostream> #include <string> using namespace std; // 定义产品类 Product class Product { public: string name; int quantity; // 构造函数 Product(string n, int q) : name(n), quantity(q) {} // 描述产品 void describe() { cout << "Name: " << name << ", Quantity: " << quantity << endl; } }; // 定义仓库类 Warehouse class Warehouse { private: map<string, Product> inventory; // 存储产品的哈希映射 public: // 向仓库添加产品 void addProduct(Product p) { inventory[p.name] = p; cout << "Product " << p.name << " added to the warehouse." << endl; } // 从仓库移除产品 void removeProduct(string productName) { if (inventory.find(productName) != inventory.end()) { inventory.erase(productName); cout << "Product " << productName << " removed from the warehouse." << endl; } else { cout << "Product not found in the inventory." << endl; } } // 显示仓库库存 void displayInventory() { for (const auto &item : inventory) { item.second.describe(); } } }; int main() { Warehouse farmWarehouse; // 创建并添加产品示例 Product corn("Corn", 100); farmWarehouse.addProduct(corn); // 展示库存 farmWarehouse.displayInventory(); // 入库其他产品或移除产品可在此处进行 return 0; } ``` 在这个例子中,`Product` 类代表农场的产品,如玉米,`Warehouse` 类则表示农场的仓库,包含了添加、移除产品和查看库存的方法。你可以根据农场的实际需求扩展这个基础框架,比如添加批次信息、库存检查功能等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值