zoukankan      html  css  js  c++  java
  • 20212022年寒假学习进度08

    今天继续学习scala

    今天主要了解了泛型的使用,并且学习了vue,后续会总结vue的学习

    1 协变和逆变
    1)语法
    class MyList[+T]{ //协变
    }
    class MyList[-T]{ //逆变
    }
    class MyList[T] //不变
     
    2)说明
    协变:Son 是 Father 的子类,则 MyList[Son] 也作为 MyList[Father]的“子类”。
    逆变:Son 是 Father 的子类,则 MyList[Son]作为 MyList[Father]的“父类”。
    不变:Son 是 Father 的子类,则 MyList[Father]与 MyList[Son]“无父子关系”。
    3)实操
    //泛型模板
    //class MyList<T>{}
    //不变
    //class MyList[T]{}
    //协变
    //class MyList[+T]{}
    //逆变
    //class MyList[-T]{}
    class Parent{}
    class Child extends Parent{}
    class SubChild extends Child{}
    object Scala_TestGeneric {
    def main(args: Array[String]): Unit = {
    //var s:MyList[Child] = new MyList[SubChild]
    }
    }
     
    2 泛型上下限
    1)语法
    Class PersonList[T <: Person]{ //泛型上限
    }
    Class PersonList[T >: Person]{ //泛型下限
    }
    2)说明
    泛型的上下限的作用是对传入的泛型进行限定。
    3)实操
    class Parent{}
    class Child extends Parent{}
    class SubChild extends Child{}
    object Scala_TestGeneric {
    def main(args: Array[String]): Unit = {
    //test(classOf[SubChild])
    //test[Child](new SubChild)
    //泛型通配符之上限
    //def test[A <: Child](a:Class[A]): Unit ={
    // println(a)
    //}
    //泛型通配符之下限
    //def test[A >: Child](a:Class[A]): Unit ={
    // println(a)
    //}
    //泛型通配符之下限 形式扩展
    def test[A >: Child](a:A): Unit ={
    println(a.getClass.getName)
    }
    }
     
    3 上下文限定
    1)语法
    def f[A : B](a: A) = println(a) //等同于 def f[A](a:A)(implicit arg:B[A])=println(a)
    2)说明
    上下文限定是将泛型和隐式转换的结合产物,以下两者功能相同,使用上下文限定[A :
    Ordering]之后,方法内无法使用隐式参数名调用隐式参数,需要通过 implicitly[Ordering[A]]
    获取隐式变量,如果此时无法查找到对应类型的隐式变量,会发生出错误。
    implicit val x = 1
    val y = implicitly[Int]
    val z = implicitly[Double]
     
    3)实操
    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)
     
     
    作者:哦心有
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    Fractal
    加工生产调度/爬山Mountain Climbing
    魔板 Magic Squares
    USACO Section 1.5
    USACO Section 1.4(有技巧的枚举)
    卷积神经网络 CNN
    优化算法
    评价分类器的好坏
    梯度消失 / 梯度爆炸以及Xavier初始化
    Dropout
  • 原文地址:https://www.cnblogs.com/haobox/p/15779584.html
Copyright © 2011-2022 走看看