面向对象通识20(函数式接口)

面向对象通识20(函数式接口)

概述函数式接口

引例:函数式接口:只有一个抽象方法的接口

@FunctionalInterface
//用于修饰函数式接口,作用域之前讲的@Override类似,都是用于报错
//当接口内有两个
public interface Walkable {
    public void move();
    static int sum(int a,int b){
        return a+b;
    }//实例方法1
    default void foo(){

    }//实例方法2
}

Lambda表达式

语法格式

public class LambdaTest {
    public static void main(String[] args) {
        Walkable mv=new Walkable() {
            @Override
            public void move() {
                //类体通常是实现抽象方法
                System.out.println("Just walk");
            }
        };

        //mv的编译类型是Walkable
        //Walkable声明了几个方法,mv就可以调用几个方法
        mv.move();
        mv.foo();
        Walkable md=new Walkable() {
            @Override
            public void move() {

            }
        };
        //此为Lambda表达式
        Walkable mc=()->{

        };
         /*上述等同于
         Walkable mc=new Walkable() {
            @Override
            public void move() {

            }
        };
        */
    }
}

综上可见Lambda表达式本质就是函数式接口的匿名内部类

——只保留匿名内部类所实现的抽象方法,并且只留方法的形参列表和方法体

在此可以理解为一个匿名内部类直接指向一个函数式接口唯一存在的抽象方法

接口名 对象名=(形参列表)->{
	//抽象方法所需重写的方法体部分
}

Lambda表达式的简化写法

  • 通常写形参列表的时候都省略写形参类型

    举例:

    @FunctionalInterface
    public interface Runable {
        void runner(String name,int age);
    }
    
    @FunctionalInterface
    public interface Jumpable {
        void jump(double height);
    }
    
    public class RunLambdaTest {
        public static void main(String[] args) {
            Runable a=(name, age) -> {
                System.out.println("省略形参类型"+name+age);
            };
            a.runner("a",3);
            Jumpable b=height -> System.out.println("高度"+height);
            b.jump(2.3);
        }
    }
    
  • 当函数式接口抽象方法的形参列表只有一个参数时,Lambda表达式形参列表的参数类型和括号都可以省略

  • 当函数式接口抽象方法的方法体中仅有一句语句时,Lambda表达式方法体的花括号也可以省去

  • 当函数式接口抽象方法的方法体中仅有一句语句,且这条语句是return语句,可以省略return关键字

public interface Lambda3 {
    int value(int a);
}
public class LambdaTest3 {
    public static void main(String[] args) {
        //省略形参类型,形参名,省略花括号,省略return关键字
        Lambda3 n1=n->n*n;
        System.out.println(n1.value(3));
    }
}
/*
	9	
*/

Lambda表达式所表示的方法引用和构造器引用

省略引用类方法

  • 引用类方法——只有当方法体只有一条代码的时候才可以使用
  • 本质——进一步省略,完全省略了形参列表和箭头

原有:(参数列表)->某个类.某个方法

省略后:某个类::某个方法

举例:

public interface Fn {
    double call(double a,double b);
}
public class 引用类方法 {
    public static void main(String[] args) {
        Fn fn1=(d1,d2)->Math.hypot(d1,d2);
        System.out.println(fn1.call(3.0,4.0));
        Fn fn2=Math::hypot;
        /*  与上一行本质相同
            Fn fn2=(d1,d2)->Math.hypot(d1,d2);
        */
        System.out.println(fn2.call(3,4));
    }
}

省略引用实例方法

原有:(参数1,其他参数)->参数1.某个方法(其他参数)

省略后:参数1的类型::某个方法

举例:

@FunctionalInterface
public interface Converter {
    String change(String a, int b, int c);
}
public class 引用实例方法 {
    public static void main(String[] args) {
        Converter str=(a,b,c)->a.substring(b,c);
        Converter str1=String::substring;
        System.out.println(str.change("java",1,3));
        System.out.println(str1.change("python",2,4));
    }
}

省略引用特定对象的实例方法

原有:(参数列表)->某个对象.某个方法(参数列表)

简化:某个对象::某个方法

举例:

@FunctionalInterface
public interface Conter {
    String choose(int a,int b);
}
public class 引用特殊的实例方法 {
    public static void main(String[] args) {
        Conter c1=(b,c)->"another one".substring(b,c);
        //同样省略了形参列表和箭头
        Conter c2="anther two"::substring;
        System.out.println(c1.choose(2,4));
        System.out.println(c2.choose(2,4));
    }
}

构造器引用

原有:参数列表->new 构造器(参数列表)

简化后:构造器名::new

举例:

import java.util.Date;

@FunctionalInterface
public interface Test {
    Date cal(int a, int b, int c);
}
import java.util.Date;

public class TestTest {
    public static void main(String[] args) {
        Test a1=(a,b,c)->new Date(a,b,c);
        System.out.println(a1.cal(109,3,7));
        Test a2=Date::new;
        System.out.println(a2.cal(122,3,20));
    }
}
/*
	Tue Apr 07 00:00:00 CST 2009
	Wed Apr 20 00:00:00 CST 2022
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值