java中的this::

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

::是java8 中新引入的运算符

  • Class::function的时候function是属于Class的,应该是静态方法。
  • this::function的funtion是属于这个对象的。
  • 注意:
    • 调用方法可以名称不一样,但是需要参数一样(返回值无影响)

/**
 * @author Jly
 * @date 2019/3/26  14:58
 */
public class Test {

    protected interface FunctionEx {
        void apply(String a);
    }

    public void adddd(String a){
        System.out.println("adddd---" + a);
        add(a,this::doAdd);
    }

    public String doAdd(String a){
        System.out.println("doAdd---" + a);
        return "";
    }

    public void add(String a,FunctionEx functionEx){
        System.out.println("add---" + a);
        functionEx.apply(a);  // 其实这里执行的就是doAdd
    }

    public static void main(String[] args) {
        Test test = new Test();
        System.out.println("main---");
        test.adddd("Jly");
    }
}

执行结果:

java中的this::_第1张图片

 

转载于:https://my.oschina.net/u/3847203/blog/3028012

你可能感兴趣的:(java中的this::)