1 Matching Annotations
  1. Aug 2022
    1. 改为var后,编译器就会立马报错

      fun main() { // 找到一家肯德基 // ↓ val kfc = Restaurant<KFC>() // 需要普通饭店,传入了肯德基,编译器报错 orderFood(kfc) val success:Result.Success<Cat> = Result.Success<Cat>(Cat()) println(success.data) var s:Result.Success<Animal> = success s.data = Dog() println(success.data as Cat) }

      sealed class Result<out R> { // 协变 ① // ↓ ↓ data class Success<out T:Animal>(var data: @UnsafeVariance T, val message: String = "") : Result<T>()

      data class Error(val exception: Exception) : Result<Nothing>()
      
      data class Loading(val time: Long = System.currentTimeMillis()) : Result<Nothing>()
      

      }