• 详解 Scala 的泛型


    一、协变与逆变

    1. 说明

    • 协变:Son 是 Father 的子类,则 MyList[Son] 也作为 MyList[Father] 的 “子类”
    • 逆变:Son 是 Father 的子类,则 MyList[Son] 作为 MyList[Father] 的 “父类”
    • 不变:Son 是 Father 的子类,则 MyList[Father] 与 MyList[Son] 之间 “无父子关系“

    2. 语法

    // 不变
    class MyList[T]{}
    
    // 协变
    // class MyList[+T]{}
    
    // 逆变
    // class MyList[-T]{}
    
    class Parent{}
    class Child extends Parent{}
    class SubChild extends Child{}
    
    object TestGenerics {
        def main(args: Array[String]): Unit = {
            // 不变
            var mylist1: MyList[Child] = new MyList[Child]
            // var mylist2: MyList[Parent] = new MyList[Child] // error,无父子关系
            
            // 协变
            // var mylist1: MyList[Child] = new MyList[Child]
            // var mylist2: MyList[Parent] = new MyList[Child]
            // var mylist3: MyList[Child] = new MyList[SubChild]
            
            // 逆变
            // var mylist1: MyList[Child] = new MyList[Child]
            // var mylist2: MyList[Child] = new MyList[SubChild] // error, 父子关系逆转
            // var mylist3: MyList[SubChild] = new MyList[Child]
            
        }
    }
    

    二、泛型上下限

    泛型的上下限的作用是对传入的泛型进行限定

    /**
    	[T <: Class]:泛型上限,类型 T 只能是 Class 或 Class 子类
    	[T >: Class]:泛型下限,类型 T 只能是 Class 或 Class 父类
    */
    class Parent{}
    class Child extends Parent{}
    class SubChild extends Child{}
    
    object TestGenerics {
        def main(args: Array[String]): Unit = {
            def test[A <: Child](a: A) { // 类型只能是 Child及其子类
                println(a.getClass.getName)
            }
            
            test[Child](new Child)
            test[Child](new SubChild)
            test[SubChild](new SubChild)
            // test[Parent](new Child) // error
            
        }
    }
    

    三、上下文限定

    1. 说明

    ​ 上下文限定是将泛型和隐式转换的结合产物,以下两者功能相同,使用上下文限定 [A : Ordering] 之后,方法内无法使用隐式参数名调用隐式参数,需要通过 implicitly[Ordering[A]] 获取隐式变量,如果此时无法查找到对应类型的隐式变量,会发生出错误

    2. 语法

    /**
    	def f[A: B](a: A) = println(a) 
    	//等同于 
    	def f[A](a: A)(implicit arg: B[A]) = println(a)
    */
    object TestGenerics {
        def main(args: Array[String]): Unit = {
            
            def f[A: Ordering](a: A, b: A) = implicitly[Ordering[A]].compare(a, b)
            
    		def f[A](a: A, b: A)(implicit ord: Ordering[A]) = ord.compare(a, b)
        }
    }
    
  • 相关阅读:
    Java动态代理Proxy类
    ubuntu提高 github下载速度
    循环队列c语言版
    C++ Primer第五版_第十九章习题答案(11~20)
    NewStarCTF2023week2-R!!C!!E!!
    OSG交互:选中场景模型并高亮显示
    二、浏览器--事件循环(也叫事件环,也叫event loop)--任务队列(等待执行的任务(存放的定时器,http,事件等进程))--渲染三者的关系
    【数据结构-队列】队列介绍
    网站有上传后门木马的漏洞怎么解决
    MySQL数据库入门到大牛_05_排序ORDER BY与分页LIMIT
  • 原文地址:https://blog.csdn.net/weixin_44480009/article/details/139275294