下面是Long
實施Scala
:
object Long extends AnyValCompanion {
/** The smallest value representable as a Long. */
final val MinValue = java.lang.Long.MIN_VALUE
/** The largest value representable as a Long. */
final val MaxValue = java.lang.Long.MAX_VALUE
/** Transform a value type into a boxed reference type.
*
* Runtime implementation determined by `scala.runtime.BoxesRunTime.boxToLong`. See [[https://github.com/scala/scala src/library/scala/runtime/BoxesRunTime.java]].
*
* @param x the Long to be boxed
* @return a java.lang.Long offering `x` as its underlying value.
*/
def box(x: Long): java.lang.Long = java.lang.Long.valueOf(x)
/** Transform a boxed type into a value type. Note that this
* method is not typesafe: it accepts any Object, but will throw
* an exception if the argument is not a java.lang.Long.
*
* Runtime implementation determined by `scala.runtime.BoxesRunTime.unboxToLong`. See [[https://github.com/scala/scala src/library/scala/runtime/BoxesRunTime.java]].
*
* @param x the java.lang.Long to be unboxed.
* @throws ClassCastException if the argument is not a java.lang.Long
* @return the Long resulting from calling longValue() on `x`
*/
def unbox(x: java.lang.Object): Long = x.asInstanceOf[java.lang.Long].longValue()
/** The String representation of the scala.Long companion object. */
override def toString = "object scala.Long"
/** Language mandated coercions from Long to "wider" types. */
import scala.language.implicitConversions
implicit def long2float(x: Long): Float = x.toFloat
implicit def long2double(x: Long): Double = x.toDouble
}
當你在最後看到的,有兩個隱含的方法。方法隱式地將long
轉換爲float
。
當您向接受float
的方法證明long
時,此方法用於轉換。