package day04
// implicit 隐含的
// 柯里化
// 定义对象之间的比较规则
object ImplicitContext{
implicit object OrderingGirl extends Ordering[Girl]{
override def compare(x: Girl, y: Girl): Int = if (x.faceValue>y.faceValue) 1 else -1
}
}
// 声明自定义对象类
class Girl(val name:String,var faceValue: Int){
override def toString: String = s"name:$name,faceValue:$faceValue"
}
class Goddess[T:Ordering](val v1:T,val v2:T){
def choose()(implicit ord:Ordering[T])=if (ord.gt(v1 , v2) ) v1 else v2
}
object Goddess {
def main(args: Array[String]): Unit = {
import ImplicitContext.OrderingGirl
val g1= new Girl("Li",90)
val g2= new Girl("Wang",90)
val goddess=new Goddess(g1,g2)
val res=goddess.choose()
println(res)
}
}
[Scala] 39 柯里化、隐式转换练习
最新推荐文章于 2022-07-05 13:56:37 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)