小编jun*_*jun的帖子

覆盖 toString 时如何使用条件语句

我正在尝试使用条件语句覆盖 toString。但是我收到一个错误,指出类型不匹配。它应该是 String 类型,现在它是 Any 类型。noGoals、Tied 是返回 true 或 false 的函数。

override def toString = {
  if (this.noGoals)
    s"${this.home} vs. ${this.away} at ${this.location}: no goals"
  else if (this.Tied)
    s"${this.home} vs. ${this.away} at ${this.location}: tied"
}




    
Run Code Online (Sandbox Code Playgroud)

scala conditional-operator

2
推荐指数
1
解决办法
74
查看次数

标签 统计

conditional-operator ×1

scala ×1