码迷,mamicode.com
首页 > 其他好文 > 详细

scala-协变和逆变

时间:2018-03-22 22:33:49      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:extend   gpo   xtend   ext   div   scala   span   ==   pre   

class GaoJi

class ZhongJi extends GaoJi

//协变===========================
class Card[+T]

val cgaoji = new Card[GaoJi]
val czhongji = new Card[ZhongJi]

//协变之后Card[ZhongJi]犹如继承了Card[GaoJi]
def test(card: Card[GaoJi]): Unit = {
  println(123)
}

test(cgaoji)
test(czhongji)

//逆变===========================
class Card1[-T]

val cgaoji1 = new Card1[GaoJi]
val czhongji1 = new Card1[ZhongJi]

//逆变之后Card[GaoJi]犹如Card[ZhongJi]的父类也可被接收
def test1(card: Card1[ZhongJi]): Unit = {
  println(678)
}

test1(cgaoji1)
test1(czhongji1)

 

scala-协变和逆变

标签:extend   gpo   xtend   ext   div   scala   span   ==   pre   

原文地址:https://www.cnblogs.com/zzq-include/p/8626849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!