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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog