31DaysOfKotlin-series 2

  1. Kotlin可以有mutable和ready-only的field,同时它的getters和setters方法默认是自动生成的,你也可以自定义。

    class User {
           val id: String = "" //immutable. just getter
           var name: String = "" //default getter and setter
           var surName: String = "" //custom getter, default setter
               get() = surName.toUpperCase() //custom getter declaration
    
           var email: String = "" //default getter, customer setter
               set(value) {     // custom setter declaration
                   //"value" custom setter's parameter
                   //"field" = property's backing field;
                   if (isEmailValid()) field = value 
               }

    Properties and Fields: Getters, Setters, const, lateinit - Kotlin Programming Languagekotlinlang.org

  2. 使用data class当你想要保存一些数据的时候,它默认实现了equals,hashcode,toString以及copy

    data class User(val name: String, val email: String)
    
       class UserListDiffCallback {
           fun areContentsTheSame(oldPosition: Int, newPosition: Int): Boolean {
               // use the generated equals method
               return newList[oldPosition] == oldList[newPosition]
           }
       }

    Equality - Kotlin Programming Languagekotlinlang.org

  3. 在Kotlin里,默认的修饰符都是public,但它还是提供了一些其他的修饰符:private,protected,internal

    // public by default
    var isVisible = true
    
    // only in the same file
    private var isHidden = false
    
    internal val almostVisible = true
    
    class Foo {
       // public by default
       var isVisible = true
    
       // visible to my subclasses
       protected val isInheritable = true
    
       // only in the same class
       private val isHidden = true
    }

    Visibility Modifiers - Kotlin Programming Languagekotlinlang.org

  4. 如果过度重载导致函数的数量急剧增加,我们可以指定Kotlin中参数的default值,还可以通过在call函数时候,指定参数值,增加code的可读性

    // parameters with default values
    class BulletPointSpan(private val bulletRadius: Float = 1.2f,
                         private val gapWidth: Int = 5,
                         private val color: Int = 255)
    
    // using only default values
    val bulletPointSpan = BulletPointSpan()
    // passing a value to the first argument and using default values for the other two
    val bulletPointSpan2 = BulletPointSpan(1.6f)
    // using a named parameter for the last argument and default values for the other two
    val bulletPointSpan3 = BulletPointSpan(color = 0)

    Functions: infix, vararg, tailrec - Kotlin Programming Languagekotlinlang.org

  5. 密封类用来限制类的继承关系,这意味着密封类的子类数量是固定的。看起来就像是枚举那样,当你想在一个密封类的子类中寻找一个指定的类的时候,你可以事先知道所有的子类。不同之处在于枚举的实例是唯一的,而密封类可以有很多实例,它们可以有不同的状态。

    它和Enum class的区别:sealed class子类可以有multi instances,但是enum constant只有一个instance实例,如果需要constant behavior,选择enum,否则选择sealed;sealed类本身是抽象的,不能实例化,可以添加abstract member

    
    sealed class Intention {
       // 在kotlin1.1之前,必须放到Intention之中
       class Refresh: Intention()
       class LoadMore: Intention()
    }
    
    fun main(args: Array<String>) {
       val intention: Intention = Intention.LoadMore()
       // else statement is unnecessary when using sealed class
       val output = when (intention) {
           is Intention.Refresh -> "refresh"
           is Intention.LoadMore -> "loadMore"
       }
       println(output)
    }
    
    //另外一种方式定义
    sealed class NetworkResult
    data class Success(val result: String): NetworkResult()
    data class Failure(val error: String): NetworkResult()

    Sealed Classes - Kotlin Programming Languagekotlinlang.org

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页