scala 有 + 运算符吗? - 03

本文深入探讨了Scala语言中运算符的实现方式,揭示了看似普通的运算符如+、-、*、/等实际上被设计为方法的内部机制。通过具体的代码示例,展示了如何在Scala中使用这些运算符方法,并解释了这种设计背后的原理。

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
+ 这个符号其实是方法的名称,

转载于:https://www.cnblogs.com/daimajun/p/7224473.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值