weixin_38920641 2020-09-09 22:33 采纳率: 0%
浏览 123
已采纳

麻烦帮看下scala中两小段代码的含义

有两个问题:
1. val RichFile(a @ _*) = "/home/cay/readme.txt"中,括号中的a @ _*的字符含义
是什么?这是什么语法?
其对应的整个代码如下:

object quick1110_1 extends App {
  object RichFile {
    def unapplySeq(s: String): Option[Seq[String]] = Some(s.split("/").filter(_!=""))
  }
  val RichFile(a @ _*) = "/home/cay/readme.txt"
  println(a)
}

  1. val (d, c) = (this.toCent() - other.toCent()) /% 100这段代码的含义?(d,c)是元组吗?/%是什么运算符或者什么语法?麻烦解释下这段代码,所在的 整个代码如下:

object quick1104_2 extends App {
  class Money(val dollar: BigInt, val cent: BigInt) {
    def +(other: Money): Money = {
      val (a, b) = (this.cent + other.cent) /% 100
      new Money(this.dollar + other.dollar + a, b)
    }
    def -(other: Money): Money = {
      val (d, c) = (this.toCent() - other.toCent()) /% 100
      new Money(d, c)
    }
    private def toCent() = {
      this.dollar * 100 + this.cent
    }
    def ==(other: Money): Boolean = this.dollar == other.dollar && this.cent == other.cent
    def <(other: Money): Boolean = this.dollar < other.dollar || (this.dollar == other.dollar && this.cent < other.cent)
    override def toString = "dollar = " + dollar + " cent = " + cent
  }
  object Money {
    def apply(dollar: Int, cent: Int): Money = {
      new Money(dollar, cent)
    }
  }
  val m1 = Money(1, 200)
  val m2 = Money(2, 2)
  println(m1 + m2)
  println(m1 - m2)
  println(m1 == m2)
  println(m1 < m2)
  println(Money(1, 75) + Money(0, 50))
  println(Money(1, 75) + Money(0, 50) == Money(2, 25))
  println(Money(4,2)-Money(2,3))
}


  • 写回答

1条回答 默认 最新

  • threenewbee 2020-09-09 22:49
    关注
    @符号在scala编译中做了一个模式配置的工作,将字符串做了比对,如果值相等,将将这个值取到赋值给变量;如果值不相等,匹配不上,就报一个异常
    
    %是除法取余数
    val (d, c) = (this.toCent() - other.toCent()) /% 100
    相当于
    d = (this.toCent() - other.toCent()) / 100
    c = (this.toCent() - other.toCent()) % 100
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站