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"
  }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM