scala 中隐式转换 implicit的应用

下面是一个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)


此时,就可以正常输出了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值