Scala类型参数(泛型)与隐式转换
2024-09-08 06:44:34
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"
} }
最新文章
- GCD定时器
- Android 学习第15课,Android 开发的单元测试、及输出错误信息
- 24. Oracle 10g安装检测中DHCP报错
- 建立自己的Visual Studio工程模板
- PAT1007
- .net 学习资源(转)
- Qwt的编译与配置
- 消息映射(C++)(转)
- [python] 带有参数并且传递参数的装饰器
- 适用于typecho0.9的评论表情插件
- mysql灾备演练问题
- Unity报错 GameObject is already being activated or deactivated
- Linux命令之grep
- android studio设计模式和文本模式切换
- plsql 创建表空间、用户、赋予权限
- jQuery小案例
- Sql server中依据存储过程中的部分信息查找存储过程名称的方法【视图和Function】
- Nginx的几个常用配置和技巧
- 由于安装Android设备驱动异常,ADB无法识别安卓设备的解决方案
- MVVM - 事件转命令2
热门文章
- Task1:知识图谱介绍(1天)
- 【模拟】P1143进制转换
- 经典项目管理 OR 敏捷项目管理,我该怎么选?
- Viser报错:dodge is not support linear attribute, please use category attribute!
- centos 安装 部署 gitlab github
- 剑指offer 面试题10.1:青蛙跳台阶
- python学习笔记 | PyCharm出现卡顿解决方法
- 【Linux】dlopen failed: /lib/lsiRAID.so: cannot open shared object file: No such file or directory
- Ubuntu Terminal命令行新建仓库并推送到远程仓库
- JMM在X86下的原理与实现