scala 有运算符吗?

答案是没有。

package com.msym

/**
  * Created by ACER on 2017/7/4.
  */
object Demo {
  def main(args: Array[String]): Unit = {
    println(1 + 2)
    println(1.+(2))

  }
}
打印得到的结果都是 3,

其他的 - ,*, /, %, << 也是这样的,

在其他语言中的运算符,以及逻辑操作符,在 scala中都被定义成了方法

查看其源码:

/** Returns the sum of this value and `x`. */
  def +(x: Byte): Int
  /** Returns the sum of this value and `x`. */
  def +(x: Short): Int
  /** Returns the sum of this value and `x`. */
  def +(x: Char): Int
  /** Returns the sum of this value and `x`. */
  def +(x: Int): Int
  /** Returns the sum of this value and `x`. */
  def +(x: Long): Long
  /** Returns the sum of this value and `x`. */
  def +(x: Float): Float
  /** Returns the sum of this value and `x`. */
  def +(x: Double): Double
+ 这个符号其实是方法的名称,

相关文章:

  • 2022-01-19
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-08-14
  • 2021-07-06
  • 2021-12-17
  • 2021-10-27
猜你喜欢
  • 2021-05-24
  • 2021-09-18
  • 2023-02-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2021-12-16
相关资源
相似解决方案