//Lambda表达式用于实现只有一个抽象方法的接口,以达到简便、简化的目的
//避免创建一个类来实现接口这一繁琐的过程
//其可以代替匿名内部类
//不推荐使用,因为其简单所以其难懂,难以调试
// 1. 不需要参数,返回值为 5
() -> 5
// 2. 接收一个参数(数字类型),返回其2倍的值
x -> 2 * x
// 3. 接受2个参数(数字),并返回他们的差值
(x, y) -> x – y
// 4. 接收2个int型整数,返回他们的和
(int x, int y) -> x + y
// 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)
(String s) -> System.out.print(s)
/*此版本暂时不支持lambda表达式
package com.Fengkuangjava.Lambada;
//语法格式:([数据类型 参数名,数据类型 参数名,...])->{表达式主体}
//表达式主体:就是抽象方法的具体实现,如果表达式主体只有一条语句,那么可以省略包含主体的大括号。
//另外,在Lambda表达式中允许有返回值,当只有一条return语句时,也可以省略return关键字
public class Example1 {
interface Animal{
void shout();
}
public static void main(String[] args) {
String name = "小花";
//1.0匿名内部类作为参数传递给animalShout()方法
animalShout(new Animal() {
@Override
public void shout() {
System.out.println("xiaohuahuahauhau");
}
});
//2.0使用Lambda表达式作为参数传递给animalShout()方法
animalShout(()-> {System.out.println("sfsdfdsf")});
}
//创建一个animalShout()静态方法,接收接口类型的参数
public static void animalShout(Animal an){
an.shout();
}
}*/
/*package com.Fengkuangjava.Lambada;
//函数式接口
//接口中有且只有一个抽象方法时,才能使用Lambda表达式代替匿名内部类
//定义无参、无返回值的函数式接口
interface Animal{
void shout();
}
//定义有参、有返回值的函数式接口
interface Calculate{
int sum(int a,int b);
}
public class Example2 {
public static void main(String[] args) {
//分别两个函数式接口进行测试
animalShout(()-> {System.out.println("无参无返回值的函数式接口")});
showSum(10,20,(x,y)->x+y);
}
//创建一个动物叫的方法,并传入接口对象Animal作为参数
private static void animalShout(Animal animal){
animal.shout();
}
//创建一个求和的方法,并传入两个int类型以及接口Calculate类型的参数
private static void showSum(int x,int y,Calculate calculate){
System.out.println(calculate.sum(x,y));
}
}*/
package com.Fengkuangjava.Lambada;
//方法引用与构造器引用
/*
种类 Lambda表达式示例 对应的引用示例
类名引用普通方法 (x,y,z...)->对象名x.类普通方法名(y,z,...) 类名::类普通方法名
类名引用静态方法 (x,y,z...)->类名.类静态方法名(x,y,z...) 类名::类静态方法名
对象名引用方法 (x,y,z...)->对象名.实例方法名(x,y,z...) 对象名::实例方法名
构造器引用 (x,y,z...)->new 类名(x,y,z...) 类名::new
*/
/*//类名引用静态方法
//定义一个函数式接口
@FunctionalInterface
interface Calcable{
int calc(int num);
}
//定义一个类,并在类中定义一个静态方法
class Math{
//定义一个求绝对值的方法
public static int abs(int num){
if (num<0){
num = -num;
}
return num;
}
}
public class Example3 {
private static void printAbs(int num,Calcable calcable){
System.out.println(calcable.calc(num));
}
public static void main(String[] args) {
//使用Lambda表达式方式
printAbs(-10,n->Math.abs(n));
//使用方法引用的方式
printAbs(-10,Math::abs);
}
}*/
/*//对象名引用方法
//定义一个函数式接口
@FunctionalInterface
interface Printable{
void print(String str);
}
class StringUtils{
public void printUpperCase(String str){
System.out.println(str.toUpperCase());
}
}
//定义测试类
public class Example3{
private static void printUpper(String text,Printable pt){
pt.print(text);
}
public static void main(String[] args) {
StringUtils stu = new StringUtils();
//使用Lambda表达式
printUpper("hello",t->stu.printUpperCase(t));
//使用方法引用的方式
printUpper("hello",stu::printUpperCase);
}
}*/
/*//构造器引用方法
//构造器引用指的是对类自带的构造器的引用
//定义一个函数式接口
@FunctionalInterface
interface PersonBuilder{
Person buildPerson(String name);
}
//定义一个Person类,并添加有参构造方法
class Person{
private String name;
public Person(String name){
this.name = name;
}
public String getName(){
return name;
}
}
//定义测试类
public class Example3{
public static void printName(String name,PersonBuilder builder){
System.out.println(builder.buildPerson(name).getName());
}
public static void main(String[] args) {
//使用Lambda表达式方式
printName("打发",name->new Person(name));
//使用构造器引用的方式
printName("打发",Person::new);
}
}*/
/*//类名引用普通方法
//定义一个函数式接口
interface Printable{
void print(StringUtils su,String str);
}
class StringUtils{
public void printUpperCase(String str){
System.out.println(str.toUpperCase());
}
}
//定义测试类
public class Example3{
private static void printUpper(StringUtils su,String text,Printable pt){
pt.print(su,text);
}
public static void main(String[] args) {
//使用Lambda表达式方式
printUpper(new StringUtils(),"hello",(object,t)->object.printUpperCase(t));
//使用方法引用的方式
printUpper(new StringUtils(),"hello",StringUtils::printUpperCase);
}
}*/