Kotlin:泛型Out In关键字的理解

考虑到类型安全的原因,因此java需要泛型来解决:西瓜不是猪肉;我喜欢吃西瓜不代表我喜欢吃水果,我喜欢吃西瓜所以我也喜欢吃无籽西瓜;有人喜欢吃水果所以我给他一个西瓜;等类似问题,kotlin也类似,通过指定关键字来处理这类问题,例如下面的代码:

data class Plate<T>(var item: T)
//只需要返回值,不需要传入参数
interface Source<T>{  fun getSource():Source<T>  }
class Test{  fun <T:Fruit> setPlate(plate: Plate<T>){}  }
//out即java中的<? extends T>
//意为仅可作为返回值,返回值类型是T或T的子类
data class Basket<out T>(val item: T)
//in即java中的<? super T>
//意为仅可作为参数传入,传入的参数类型是T或T的子类
class Bowl<in T> {  fun setItem(item: T) {}  }
open class Food
open class Fruit : Food()
class Apple : Fruit()
class Banana : Fruit()
fun main() {
  val plate1 = Plate(Food())
  plate1.item = Fruit()
  val test = Test()
  //无法通过 因为Food不是Fruit或其子类
  //test.setPlate(plate1)
  val plate3 = Plate(Apple())
  test.setPlate(plate3)
  val fruit: Fruit = plate1.item as Fruit
  val basket: Basket<Fruit> = Basket<Apple>(Apple())
  //抛出异常
  //Exception in thread "main" java.lang.ClassCastException: Apple cannot be cast to Banana
  //val banana:Banana = basket.item as Banana
  val food: Food = basket.item
  val bowl: Bowl<Fruit> = Bowl()
  //抛出异常
  //Exception in thread "main" java.lang.ClassCastException: Food cannot be cast to Fruit
  //bowl.setItem(Food() as Fruit)
  bowl.setItem(Apple())
}
fun demo(fruit: Source<Fruit>){
  //不能通过,
  //val food:Source<Food> = fruit.getSource()
}
  • main()函数:plate1item能set为Fruit类型,因为FruitFood的子类,也就是:这个人喜欢吃所有食物,那他一定也喜欢吃水果。
  • demo()函数:food的类型为Source<Food>,但确不能接受类型为Source<Fruit>fruit,这显然不合理,Source接口我只需要T及其子类能作为返回值,但不需要传入T或其子类类型,也就是:我不会把食物放到水果资源当中,我只需要在我想要一个食物的时候,水果应该能被视为食物的一个子类,能够返回给我一个水果,我也可以接受。

因此,java引入了<? extends T> <? super T>,用于区分这两种情况,前者被称为生产者,也就是只能读取,后者被称为消费者,也就是只能写入。在kotlin中,则引入了<out T><in T>分别指代<? extends T><? super T>,简单通俗的讲,<out T>只能作为返回值,返回T或其子类,而<in T>可以作为参数传递,传递T及其子类。

  • main()函数:test.setPlate() 只接受Fruit及其子类,即通过<T:Fruit>来指定T的类型,也就是:这个人喜欢吃水果,但他不一定喜欢吃所有食物。但是这个人喜欢吃水果,所以他也一定喜欢吃苹果。
  • main()函数:Basket通过<out T>指定了T的类型,意即java中的<? extends T>,仅能作为返回值,意为T及其子类。也就是:这个人想要水果,苹果是食物的一种,所以我可以提供水果当作食物(返回值)给他。
  • main()函数:Bowl则与Basket相反,通过<in T>指定了T的类型,意即java中的<? super T>,用以参数传递,意为T及其子类。也就是:这个人喜欢吃水果,苹果是水果的一种,所以这个人一定也喜欢吃苹果(水果及其子类可作为参数传递)。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。