首先在jkd8中的新特性之一,在接口中可以存在被static和default关键字修饰的有方法体的方法
public interface TestInterface1 {
void f1();
// public void f2(){
//
// }
//未被static或者default修饰的方法,不能有方法体。
static void f3(){
}
default void f4(){
}
}
当一个接口被 @FunctionalInterface修饰,这个接口会成为函数式接口,函数式接口有且仅有一个抽象方法
@FunctionalInterface
public interface TestInterface2 {
void f1();
//void f2(); 若是接口是函数式接口,若是有多的抽象方法会报错,而若是没有抽象方法,同样会报错
static void f3(){
}
default void f4(){
}
}
在实现接口时,若是该接口的方法仅调用一次,常用匿名内部类的方式,避免新建一个类去实现接口,lambda表达式作为JDK8的新特性,可以使代码更加简洁,更加精简的创建匿名内部类。lambda语法:(参数列表)—>{方法体}
//正常写法
public class Lambda {
TestInterface2 test = new TestInterface2() {
@Override
public void f1() {
System.out.println("创建匿名内部类");
}
};
}
//lambda写法
//首先去掉new对象的部分和方法名,仅保留参数和方法体并添加箭头,用一对大括号包住方法体
public class Lambda {
TestInterface2 test = ()-> {
System.out.println("创建匿名内部类");
};
}
//当方法体只有一行代码,可再次简化,去掉外部大括号,仅用参数括号和箭头、方法体。
public class Lambda {
TestInterface2 test = () -> System.out.println("创建匿名内部类");
}
当接口中的方法无参有返回值时。
//接口中的抽象方法无参有返回值
public interface TestInterface2 {
int f1();
}
//在类中创建匿名内部类并重写接口中的抽象方法
public class Lambda {
TestInterface2 test2 = new TestInterface2() {
@Override
public int f1() {
return 0;
}
};
}
//使用lambda表达式简化
public class Lambda {
TestInterface2 test2 = ()-> {
return 0;
};
}
//当方法体中只有一行代码时,可以去掉括号,同时去掉return
public class Lambda {
TestInterface2 test2 = ()-> 0;
}
当接口中的方法有参无返回值时
//接口中有一个有参无返回值的方法
@FunctionalInterface
public interface TestInterface2 {
void f1(int num);
}
//在类中创建匿名内部类并重写方法
public class Lambda {
TestInterface2 test3 = new TestInterface2() {
@Override
public void f1(int num) {
System.out.println(num);
}
};
}
//使用lambda表达式简化
public class Lambda {
TestInterface2 test3 = (int num)-> {
System.out.println(num);
};
}
//若是有参的情况,可以去掉参数类型
public class Lambda {
TestInterface2 test3 = (num)-> System.out.println(num);
}
//若是只有一个参时,可以省略括号
//public class Lambda {
TestInterface2 test3 = num-> System.out.println(num);
}
有参有返回值时
//接口中有多个参数有返回值的方法
@FunctionalInterface
public interface TestInterface2 {
int f1(int a, int b);
}
//在类中创建匿名内部类实现接口
public class Lambda {
TestInterface2 test4 = new TestInterface2() {
@Override
public int f1(int a, int b) {
return (a+b);
}
};
}
//使用lambda表达式简化
public class Lambda {
TestInterface2 test4 = (int a, int b)-> {
return (a+b);
};
}
//在方法体只有一行代码时可以去掉大括号和return,并且只有一种参数类型,可以去掉参数类型
//因为有两个参数,所以保留括号
public class Lambda {
TestInterface2 test4 = (a, b)-> (a+b);
}
在lambda表达式中调用构造方法
//在接口中有一个参数为String类型返回值为Integer类型的方法
@FunctionalInterface
public interface TestInterface2 {
Integer f1(String str);
}
//在类中用匿名内部类实现接口
public class Lambda {
TestInterface2 test5 =new TestInterface2() {
@Override
public Integer f1(String str) {
return new Integer(str);
}
};
}
//使用lambda表达式简化
public class Lambda {
TestInterface2 test5 =(String str) ->{
return new Integer(str);
};
}
//去掉大括号和retrun,因为只有一个参数和一种参数类型,去掉参数类型和括号
public class Lambda {
TestInterface2 test5 =str -> new Integer(str);
}
//最终的简化,类名::new
public class Lambda {
TestInterface2 test5 =Integer::new;
}
在lambda表达式中调用静态方法
//接口中有一个返回值为int类型,参数为String类型的抽象方法
@FunctionalInterface
public interface TestInterface2 {
int f1(String str);
}
//在类中使用匿名内部类实现方法
public class Lambda {
TestInterface2 test6 = new TestInterface2() {
@Override
public int f1(String str) {
return Integer.parseInt(str);
}
};
}
//lambda表达式简化
public class Lambda {
TestInterface2 test6 = (String str)->{
return Integer.parseInt(str);
};
}
//继续简化
public class Lambda {
TestInterface2 test6 = str-> Integer.parseInt(str);
}
//最终简化
public class Lambda {
TestInterface2 test6 =Integer::parseInt;
}