scala 中隐式转换 implicit的应用
来源:互联网 发布:windows未正确加载 编辑:程序博客网 时间:2024/04/29 17:38
下面是一个Rational的类
class Rational(n: Int, d: Int) { require(d != 0) private val g = gcd(n.abs, d.abs) val numer: Int = n / g val denom: Int = d / g def this(n: Int) = this(n, 1) override def toString = numer + "/" + denom def add(that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) def lessThan(that: Rational) = this.numer * that.denom < that.numer * this.denom def max(that: Rational) = if (this.lessThan(that)) that else this private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a%b) def + (that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) def + (i: Int): Rational = new Rational(numer + i * denom, denom) def * (that: Rational): Rational = new Rational( numer * that.numer, denom * that.denom ) def * (i: Int): Rational = new Rational(numer * i, denom) def - (i: Int):Rational = new Rational(numer - i * denom, denom) def - (that: Rational): Rational = new Rational( numer * that.denom - that.numer * denom, denom * that.denom ) def / (that: Rational):Rational = new Rational(numer * that.denom, denom * that.numer) def / (i: Int): Rational = new Rational(numer, denom * i)}
下面简单的使用一下Rational类
object Test extends App{ val x = new Rational(1,2) val y = new Rational(2,3) <pre name="code" class="plain"> <span style="color:#009900;">println(y * 2)</span>
}可以正常输出结果:4/3。
我们把输出的改为:
object Test extends App{ val x = new Rational(1,2) val y = new Rational(2,3) println(2 * y)}会报如下,错误:
overloaded method value * with alternatives: (x: Double)Double <and> (x: Float)Float <and> (x: Long)Long <and> (x: Int)Int <and> (x: Char)Int <and> (x: Short)Int <and> (x: Byte)Int cannot be applied to (mytest.Rational)
原因是:
The problem here is that 2 * r is equivalent to 2.*(r), so it is a method
call on the number 2, which is an integer. But the Int class contains no
multiplication method that takes a Rational argument—it couldn’t because
class Rational is not a standard class in the Scala library.
However, there is another way to solve this problem in Scala: You can
create an implicit conversion that automatically converts integers to rational
numbers when needed. Try adding this line in the interpreter:
这个时候就需要隐式转换
object Test extends App{ val x = new Rational(1,2) val y = new Rational(2,3) implicit def intToRational(x: Int) = new Rational(x) //隐式转换 println(2 * y)
此时,就可以正常输出了。
- scala 中隐式转换 implicit的应用
- Scala中隐式转换(implicit conversion)的优先顺序
- Scala中隐式转换(implicit conversion)的优先顺序
- Scala中隐式转换(implicit conversion)的优先顺序
- Scala中隐式转换(implicit conversion)的优先顺序
- scala implicit 应用相关
- scala implicit 隐式转换
- scala implicit隐式转换
- Scala implicit隐式转换
- scala implicit 隐式转换
- scala implicit 隐式转换
- scala implicit 隐式转换
- scala implicit 隐式转换
- scala隐式转换implicit
- Scala的implicit
- 关于Scala的implicit(隐式转换)的思考
- scala的implicit关键字----不仅仅是隐式类型转换
- Scala学习之五隐式转换implicit
- 编程语言的选择原则
- mysql 性能调优
- Spark Streaming容错的改进和零数据丢失
- 路遥短篇小说之《匆匆过客》
- Restrictions HQL运算符对应
- scala 中隐式转换 implicit的应用
- IAR生成文件解析,方便保存工程
- Shader博客
- System.out.print和System.out.println的区别
- NGUI创建Camera参数为Simple 2D的UI UI对象的结构UI Root(2D)
- 阿里、腾讯以及一些创业公司是如何进行研发管理和绩效考核的?
- 领域驱动设计之领域模型
- atoi函数实现
- IOS开发之---ARC(Automatic Reference Counting)