在通过传递Long值调用方法时,为什么Float参数方法被执行了?

时间:2017-09-26 08:43:34

标签: scala

class Demo  
{
     def m1(a: Float) 
    {
        println("m1 Float-arg method");
    }
}

object Demo1     
{
    def main(args: Array[String])
    {
        val demo = new Demo

        demo.m1(10f)
        demo.m1(65l)
    }
}

输出:

m1 Float-arg方法

m1 Float-arg方法

  1. 如上例所示,我使用Long值调用m1(65l)方法,但我没有使用Long参数定义m1方法,所以我预期错误,

    < / LI>
  2. 但为m1(56l)方法调用执行了带浮点参数方法的m1方法?

  3. 内部流程将如何?

    任何人都可以解释一下吗?

    由于

2 个答案:

答案 0 :(得分:3)

IF callcontrol-step = 'DISP'. TYPES: BEGIN OF ls_result, apofasi LIKE zsl_glk_apof-apofasi, apofasidate LIKE zsl_glk_apof-apofasidate, apofasisap LIKE zsl_glk_apof-apofasisap, apofasi_trp_x LIKE zsl_glk_apof-apofasi_trp_x, apofasi_tr_x LIKE zsl_glk_apof-apofasi_tr_x, fek LIKE zsl_glk_apof-fek, katdanl LIKE zsl_glk_apof-katdanl, reference LIKE zsl_glk_apof-reference, skopos LIKE zsl_glk_apof-skopos, thema LIKE zsl_glk_apof-thema, type_desc LIKE zsl_glk_apof-type_desc, ya_first LIKE zsl_glk_apof-ya_first, END OF ls_result. DATA: lt_result TYPE STANDARD TABLE OF ls_result. CLEAR: lt_result, record_tab, record_tab[]. * Read the value that user gave READ TABLE shlp-selopt INTO wa_shlp_selopt WITH KEY shlpfield = 'APOFASI'. lv_apofasi = wa_shlp_selopt-low. IF lv_apofasi <> ''. * Clear this value wa_shlp_selopt-low = ''. MODIFY shlp-selopt FROM wa_shlp_selopt INDEX sy-tabix. ENDIF. * Find the number starting APOFASISAP from the APOFASI text SELECT SINGLE apofasisap INTO lv_apofasisap_arx FROM zsl_glk_apof WHERE apofasi = lv_apofasi. IF sy-subrc = 0. * Find the APOFASISAPs which changes the starting one and display the *fields SELECT a~apofasi a~apofasidate a~apofasisap a~apofasi_tr_x a~apofasi_trp_x a~thema a~fek a~reference a~ya_first INTO CORRESPONDING FIELDS OF TABLE lt_result FROM zsl_glk_apof AS a INNER JOIN zsl_glk_apof_tr AS b ON a~apofasisap = b~apofasisap_tr WHERE b~apofasisap_trp = lv_apofasisap_arx. IF sy-subrc = 0. *Pass them to display the result. CALL FUNCTION 'F4UT_RESULTS_MAP' EXPORTING * SOURCE_STRUCTURE = apply_restrictions = 'X' TABLES shlp_tab = shlp_tab record_tab = record_tab source_tab = lt_result CHANGING shlp = shlp callcontrol = callcontrol EXCEPTIONS illegal_structure = 1 OTHERS = 2 . IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDIF. FREE lt_result. ENDIF. 随播广告对象中有隐式转化:

Long.scala

同样在implicit def long2float(x: Long): Float = x.toFloat中有自动装箱和自动装箱的转换:

Predef.scala

首先,您的implicit def long2Long(x: Long) = java.lang.Long.valueOf(x)自动退回到65l,然后转换为Long(65)

答案 1 :(得分:0)

以下是Long的{​​{1}}实施:

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 } 方法隐式将long2float转换为long

当您向接受float的方法证明long时,此方法用于转化。