JDK8+的方法引用

方法引用

1.1、基本概念

引用运算符::,

  • 它所在的表达式称为方法引用。
  • 对Lambda表达式的简化。

1.2、基本使用

@FunctionalInterface
public interface Test {
    void print(String s);
}

public class DemoFunctionQuete {
    public static void main(String[] args) {
        demo01(s-> System.out.println(s));
        demo01(System.out::println);
    }

    public static void demo01(Test p){
        p.print("test");
    }
}

1.3、各种使用方式

  • 通过对象名来引用成员方法
  • 通过类名引用静态成员方法
  • supper引用父类的成员方法
  • this引用本类成员方法
  • 类的构造方法引用
import com.sun.tools.javac.code.Attribute;

public class DemoFunctionQuete {
    private String test;

    public DemoFunctionQuete(String test) {
        this.test = test;
    }

    public DemoFunctionQuete() {
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

    @FunctionalInterface
    public interface Printable {
        void print(String s);
    }

    @FunctionalInterface
    public interface Calcable{
        int calsAbs(int number);
    }


    @FunctionalInterface
    public interface Greetable {
        void greet();
    }

    @FunctionalInterface
    public interface Runnable{
        void run();
    }

    @FunctionalInterface
    public interface DemoBulder{
        DemoFunctionQuete builderDemo(String test);
    }

    public class MethodRerObject{
        public void printUpperCaseString(String str){
            System.out.println(str.toUpperCase());
        }
    }

    public class Human{
        public void say(){
            System.out.println("human class.");
        }
        public void runSpeed(){
            System.out.println("run spped.");
        }
        public void runPark(Runnable r){
            r.run();
        }
        public void runProcess(){
            /*runPark(()->{
                this.runSpeed();
            });
             */
            runPark(this::runSpeed);
        }
    }

    public class Man extends Human{
        @Override
        public void say() {
            System.out.println("man class.");
        }

        public void method(Greetable g){
            g.greet();
        }

        public void show(){
            /*
            method(()->{
                Human h = new Human();
                h.say();
            });
             */
            // method(()->super.say());
            method(super::say);
        }
    }

    public static int getAbs(int number,Calcable c){
        return c.calsAbs(number);
    }

    public static void printTest(String test,DemoBulder db){
        DemoFunctionQuete d = db.builderDemo(test);
        System.out.println(d.getTest());
    }


    public static void main(String[] args) {

        /**********************
        通过对象名来引用成员方法
        调用的printString方法,方法的参数Printable是一个函数式接口所以可以传递lambda表达式
         */
        printString((s)->{
            MethodRerObject mro = new DemoFunctionQuete().new MethodRerObject();
            //调用成员方法printUpperCaseString
            mro.printUpperCaseString(s);
        });
        //方法引用方法
        MethodRerObject mro = new DemoFunctionQuete().new MethodRerObject();
        printString(mro::printUpperCaseString);

        /**********************
        通过类名引用静态成员方法
         */
        int number1 = getAbs(-1,n->Math.abs(n));
        System.out.println(number1);
        //方法引用方法
        int number2 = getAbs(-2,Math::abs);
        System.out.println(number2);

        /**********************
         supper引用父类的成员方法
         this引用本类成员方法
         */
        new DemoFunctionQuete().new Man().show();
        new DemoFunctionQuete().new Human().runProcess();

        //printTest("test",test->new DemoFunctionQuete(test));
        /**********************
         类的构造方法引用
         */
        printTest("test",DemoFunctionQuete::new);


    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值