Scala类型参数(泛型)与隐式转换

package com.yz9

import org.junit.Test

import scala.collection.mutable.ListBuffer

class test {
  @Test
  def test1(): Unit ={
    val abc = new Abc[String,Int]("小明",20)
    println(abc.name)
    println(abc.age)

  }

  @Test
  def test2(): Unit ={
    val list = ListBuffer[String]()
    list.append("小","大")


     for(s<- list){
       println(s)

     }


  }


  @Test
  def test3(): Unit ={
    //在函数上使用泛型
    def show[T,S,J](a:T,b:S)=println(s"$a------$b")
    show("小白",333)
  }

  @Test
  def test4(): Unit ={
    val st = new student[Int](1,66)
    println(st.bigger)  //Int不行 加上隐士转换可以

    val st2 = new student[String]("a","b")
    println(st2.bigger)//

    val st3 = new student[Long](3,55)
    println(st3.bigger)//Long不行  加上隐士转换可以

    val st4 = new student[Integer](5,66)
    println(st4.bigger)//Integer(Java类型)可以
  }

  @Test
  def test5(): Unit ={
    //scala本身不支持泛型 协变和逆变
    //要想使用 协变 (java向上转型)  泛型前写+
    // 逆变 (java向下转型) 泛型前写-

    //val list1:MyList[Person]=new MyList[St]//协变ok
    //val list2:MyList[St]=new MyList[Person]//逆变编译就报错


    val list3:YouList[St]=new YouList[Person]//逆变ok
   // val list4:YouList[Person]=new YouList[St]//协变编译报错
  }

  @Test
  def tset6(): Unit ={
    //类型通配符
    //定义一个方法
    def show(p:Pair[_<:Person]): Unit ={
      println("ok")

    }
    //调用
    show(new Pair[Person](new St,new Tc))

  }




}
class  Pair[T](first:T,second:T){
  println("运行成功")
}
class Person
class St extends  Person
class Tc extends Person
class MyList[+T]//支持协变
class YouList[-T]//支持逆变


//给T一个上限 T<:类型     Int和Long不行
// T<% 类型   %可以促使隐士转换 例如Int-》RichInt
//Comparable的子类一定有compareTo方法
class student[T <%Comparable[T]](first:T,second:T){
  //"".compareTo()字符串有该方法
  //66.compareTo(77)long有该方法
  //取出大值
 def bigger= if (first.compareTo(second)>0) first else second

}

case class Abc[T,S](name:T,age:S)//T和S只是类型的占位,使用时再指定
package com.yz9

import java.io.File

import org.junit.Test

import scala.io.Source

class test2 {

  @Test
  def test1(): Unit ={
    //val x:Int=3.5  报错

    //定义一个隐士转换函数
   implicit def double2int(num:Double):Int=num.toInt

    val a:Int=3.3//隐式(看不出来)转换
    println(a)//3
  }

  @Test
  def test2(): Unit ={
    //定义一个读文件方法
    implicit  def file2myFile(file: File):MyFile =new MyFile(file)


    //使用file调用MyFile的方法,依赖了隐士转换[相当于为file对象增加了一个方法,丰富了类库api]
    new File("C:\\Users\\a\\Desktop\\abc.txt").read()


    //做隐士转换函数时注意:
    //1 有一个单个参数(需要转谁把谁当参数)
    //2 返回值就是需要转型的目标类型



  }
  //定义一个class
  class MyFile(file: File){
    def read()= println(Source.fromFile(file).getLines().mkString)
  }



}
package com.yz9

import java.io.File

import scala.io.Source

object MyImplicitFunction {
  //要集中放在object里,不然引用时找不到

  //定义隐式函数
 implicit def show(x:Double)=x.toInt

  //定义一个读文件方法
  implicit  def file2myFile(file: File):MyFile =new MyFile(file)
//定义一个class
class MyFile(file: File){
  def read()= println(Source.fromFile(file).getLines().mkString)
}

//隐式值
  implicit val a:Int=999


  implicit def string2int(x:String): Int =x.toInt

}
package com.yz9

import java.io.File

import org.junit.Test

class test3 {
  @Test
  def test1(): Unit ={




    import com.yz9.MyImplicitFunction._//引用
    val a:Int=33.3//有隐士转换,所有的double都能当作int来用
    new File("C:\\Users\\a\\Desktop\\abc.txt").read()



  }

  @Test
  def test2(): Unit ={
    import com.yz9.MyImplicitFunction._
    //隐士参数    会被隐式值自动赋值
    def show(name:String)(implicit age:Int): Unit ={
      println(s"$name----$age")
    }

    show("张三")
    val x:Int="33"
  }

}

 

posted @ 2019-09-04 22:17  勤奋的园  阅读(963)  评论(0编辑  收藏  举报