美文网首页
Kotlin - lateinit vs lazy

Kotlin - lateinit vs lazy

作者: galaxy_zheng | 来源:发表于2019-11-17 17:41 被阅读0次

    (翻译)

    学习Kotlin——lateinit vs lazy

    lateinit vs lazy

    在Kotlin中有许多优秀的特性,我们可以利用所有这些特性在Kotlin中编写更好的应用程序。在所有这些特性中,lateinit和lazy是重要的属性初始化特性。我们必须知道何时使用哪个属性初始化。

    Kotlin 属性初始化

    如果您不想在构造函数中初始化属性,那么这是Kotlin中属性初始化的两种重要方法。

    • lateinit
    • lazy

    lateinit

    lateinit是后期初始化。

    通常,声明为非空类型的属性必须在构造函数中初始化。然而,这通常是不方便的。例如,可以通过依赖项注入或在单元测试的设置方法中初始化属性。在这种情况下,您不能在构造函数中提供非空初始化器,但在引用类体内的属性时,仍然希望避免空检查。

    要处理这种情况,可以使用lateinit修饰符标记属性。

    例子

    public class Test {
    
      lateinit var mock: Mock
    
      @SetUp fun setup() {
         mock = Mock()
      }
    
      @Test fun test() {
         mock.do()
      }
    }
    

    修饰符只能用于类主体中声明的var属性(不在主构造函数中),并且只有在属性没有自定义的getter或setter时才可以使用。属性的类型必须是非空的,并且不能是原始类型。

    lazy

    lazy 就是延迟初始化。

    lazy()是一个接受lambda并返回一个lazy实例的函数,该实例可以作为实现lazy属性的委托:对get()的第一个调用执行传递给lazy()的lambda并记住结果,对get()的后续调用只返回记住的结果。

    例子

    public class Example{
      val name: String by lazy { “Amit Shekhar” }
    }
    

    所以第一次调用和随后的调用,名字会返回" Amit Shekhar "

    现在,如何选择何时使用哪一个?

    • lazy只能用于val属性,而lateinit只能应用于vars,因为它不能编译成final字段,因此不能保证不变性。
    • lateinit变量可以在任何对象被看到的地方初始化。如果您希望以一种可能事先未知的方式从外部初始化您的属性,请使用lateinit。

    (原文)

    Learn Kotlin —  lateinit vs lazy

    lateinit vs lazy

    There are many great features available in Kotlin, we can take advantage of all these features to write the better application in Kotlin. Among all those features, lateinit and lazy are important property initialization feature. We must know when to use which property initialization.

    Kotlin Property Initialization

    If you do not want to initialize a property in the constructor, then these are two important ways of property initialisation in Kotlin.

    • lateinit
    • lazy

    lateinit

    lateinit is late initialization.

    Normally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient. For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class.

    To handle this case, you can mark the property with the lateinit modifier.

    Example

    public class Test {
    
      lateinit var mock: Mock
    
      @SetUp fun setup() {
         mock = Mock()
      }
    
      @Test fun test() {
         mock.do()
      }
    }
    

    The modifier can only be used on var properties declared inside the body of a class (not in the primary constructor), and only when the property does not have a custom getter or setter. The type of the property must be non-null, and it must not be a primitive type.

    lazy

    lazy is lazy initialization.

    lazy() is a function that takes a lambda and returns an instance of lazy which can serve as a delegate for implementing a lazy property: the first call to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get() simply return the remembered result.

    Example

    public class Example{
      val name: String by lazy { “Amit Shekhar” }
    }
    

    So the first call and the subsequent calls, name will return “Amit Shekhar”

    Now, How to choose when to use which one?

    • lazy can only be used for val properties, whereas lateinit can only be applied to vars because it can’t be compiled to a final field, thus no immutability can be guaranteed.
    • lateinit var can be initialized from anywhere the object is seen from. If you want your property to be initialized from outside in a way probably unknown beforehand, use lateinit.

    原文链接 https://blog.mindorks.com/learn-kotlin-lateinit-vs-lazy

    相关文章

      网友评论

          本文标题:Kotlin - lateinit vs lazy

          本文链接:https://www.haomeiwen.com/subject/rlopictx.html