跳到主要内容
文茂权 / DaftKen
返回

Dart-Lazy-Initialization

Dart 语言中类变量的延迟初始化

类变量存在延迟初始化行为,只有第一次调用时,类变量才执行初始化,即第一次尝试读取它时。

  // 类变量存在延迟初始化行为,只有第一次调用时,类变量才执行初始化,即第一次尝试读取它时。
  // Class variables are initialized lazily; the initializer of a class variable is executed the first time its getter is invoked—that is, the first time one attempts to read the variable.

  class Animal {
    Animal() {
      print("Start Lazy Initialization");
    }
  }
  Animal animal = new Animal();

  void main() {
  //   animal; // This statement will call the initializer;
    print("Before Initialization");
    animal;
    print("After Initialization");
  }

在 DartPad 运行此代码



上一篇
初识 Firebase
下一篇
Dart-Equal-Explore