scala 有运算符吗?
答案是没有。
打印得到的结果都是 3,package com.msym /** * Created by ACER on 2017/7/4. */ object Demo { def main(args: Array[String]): Unit = { println(1 + 2) println(1.+(2)) } }
其他的 - ,*, /, %, << 也是这样的,
在其他语言中的运算符,以及逻辑操作符,在 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
本文深入探讨了Scala语言中运算符的实现方式,揭示了看似普通的运算符如+、-、*、/等实际上被设计为方法的内部机制。通过具体的代码示例,展示了如何在Scala中使用这些运算符方法,并解释了这种设计背后的原理。
3781

被折叠的 条评论
为什么被折叠?



