zoukankan      html  css  js  c++  java
  • scala---lazy

    scala中用lazy定义的变量叫做惰性变量,会实现延迟加载。惰性变量只能是不可变的变量。并且只有在调用惰性变量的时候才会被初始化。

    class Test1 {
    
    }
    
    object Test1 {
      def main(args: Array[String]): Unit = {
        val property  = init();
        println("after init function")
        println(property)
      }
      def init(): Unit = {
        println("init function invoked!");
      }
    
    }
    正常非惰性变量的执行结果为:

      init function invoked!
      after init function
      ()

     当property被声明为惰性变量时的代码如下

    class Test1 {

    }

    object Test1 {
    def main(args: Array[String]): Unit = {
    lazy val property = init();
    println("after init function")
    println(property)
    }
    def init(): Unit = {
    println("init function invoked!");
    }

    }
    执行结果为:

    after init function
    init function invoked!
    ()

  • 相关阅读:
    Bundle Adjustment
    BFL ekf imu and odom
    RNN
    深度学习的数学(笔记)
    BP 神经网络
    PCA
    SVM
    KNN
    Kmeans
    决策树
  • 原文地址:https://www.cnblogs.com/maxigang/p/9817073.html
Copyright © 2011-2022 走看看