什么是延迟初始化,为什么是它有用吗?初始化、有用吗、为什么是

2023-09-02 12:00:39 作者:恋无可恋

什么是对象的初始化工作?你是怎么做到这一点,有什么优势?

What is lazy initialization of objects? How do you do that and what are the advantages?

推荐答案

延迟初始化是你延迟(潜在的昂贵)对象的创建,直到刚刚在你真正需要它的性能优化。

Lazy Initialization is a performance optimization where you defer (potentially expensive) object creation until just before you actually need it.

一个很好的例子是不创建数据库连接了前面,但仅仅只之前,你需要从数据库中获取数据。

One good example is to not create a database connection up front, but only just before you need to get data from the database.

键这样做的原因是,(通常),您可以完全避免创建对象,如果你永远不需要它。

The key reason for doing this is that (often) you can avoid creating the object completely if you never need it.