1.说明
    如果想要表达匹配某个范围的数据,就需要在模式匹配中增加条件守卫

    2.案例实操

    1. object TestMatchGuard {
    2. def main(args: Array[String]): Unit = {
    3. def abs(x: Int) = x match {
    4. case i: Int if i >= 0 => i
    5. case j: Int if j < 0 => -j
    6. case _ => "type illegal"
    7. }
    8. println(abs(-5))
    9. }
    10. }
    输出结果:
    5