请教下, Java 里的方法引用。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ukipoi
V2EX    Java

请教下, Java 里的方法引用。

  •  
  •   ukipoi Feb 1, 2018 2967 views
    This topic created in 3006 days ago, the information mentioned may be changed or developed.

    《 Java 核心技术 卷一》里第六章的 lambda 表达式里,有出现“ System.out::println ”的形式。这个我还能理解。 但是之后的“ Math::pow ”的形式。书里说它等价于“(x,y)->Math.pow(x,y)”,但是“ Math::pow ”的 x 和 y 的参数是那里传递进去的呢?

    4 replies    2018-02-01 21:42:04 +08:00
    JRight
        1
    JRight  
       Feb 1, 2018   1
    这两个参数是回调方法(回调方法( x,y ))里的参数,这里用两个冒号省略掉了回调方法

    如果有误请大家指出
    qinxi
        2
    qinxi  
       Feb 1, 2018
    如果看不明白的话 建议展开写,,然后利用 IDE 的 replace with lambda 转换过去

    不太恰当的例子:
    public static Double pow(Integer a, Integer b) {
    return new BiFunction<Integer, Integer, Double>() {
    @Override
    public Double apply(Integer integer, Integer integer2) {
    return Math.pow(integer, integer2);
    }
    }.apply(a,b);
    }
    ------------------- IDEA 转成下面的样子
    public static Double pow(Integer a, Integer b) {
    return ((BiFunction<Integer, Integer, Double>) (integer, integer2) -> Math.pow(integer, integer2)).apply(a,b);
    }
    --------------- 手动缩写
    public static Double pow(Integer a, Integer b) {
    return ((BiFunction<Integer, Integer, Double>) Math::pow).apply(a,b);
    }
    ---
    要求 参数顺序 类型 一致
    sudoz
        3
    sudoz  
       Feb 1, 2018
    可以理解是 Java8 的语法糖,实际上代码编译后你能看到真正执行的代码
    haozhang div class="fr">     4
    haozhang  
       Feb 1, 2018 via iPhone
    About     Help     Advertise     Blog     API     FAQ     Solana     1035 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 23:08 PVG 07:08 LAX 16:08 JFK 19:08
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86