对象方法的引用

来源:互联网 发布:淘宝企业店开店流程 编辑:程序博客网 时间:2024/06/04 21:46
/** * 对象方法引用 * ClassName:instRef <br/> * Function: TODO ADD FUNCTION. <br/> * Reason:   TODO ADD REASON. <br/> * Date:     2017年9月9日 下午1:28:06 <br/> * @author:  Lelonta * @version   * @see      *  * 类名::instmethod */public class instRef {    /**     * 抽象方法没有输入参数 就不能使用对象方法引用     *      *      * 以下都不能用     * not: (这里用一句话描述这个方法的作用).<br/>     * @author: Lelonta     */    public void not() {        Runnable runnable = () -> {};        Closeable closeable = () -> {};        Supplier<String> supplier = () -> "";    }    public static void main(String[] args) {        /**         * 对象方法的引用         * Consumer<Q,String,Integer> 与  (Q q,String str,Integer int)   最好是自定义的类型         *          |         *          ~         *        new Q()          * 第一个参数类型是恰好是自定义的Q类 ,                  剩下的参数是方法的参数         * Execute中的抽象方法             * run(W w,String name,String size)                 W中的run方法         *                   * class W                                          run(String nameString,String sizeString)                         * 泛型中的参数 与 lambda表达式中的第一个参数相同          */        Consumer<Q> c1 = (Q q) -> new Q().qq();        Consumer<Q> c2 = Q::qq;        c1.accept(new Q());        c2.accept(new Q());        BiConsumer<W, String> bc1 = W::getQ;        bc1.accept(new W(), "abc");        BiFunction<W , String, Integer> bf1 = W::getQ;        System.out.println(bf1.apply(new W(), "abcdg"));        Execute ex = (w,name,size) -> new W().run(name, size);        Execute execute = W::run;    }}class Q {    public void qq() {        System.out.println("懂了吗?");    }}class W {    public int getQ(String str) {            return str.length();        }    public void run(String nameString,String sizeString) {    }}interface Execute {    public void run(W w,String name,String size);}
阅读全文
0 0