Java8新特性-接口增强、Lambda表达式
一、Interface接口增强
1.1 默认方法
1.概念:使用default关键字来修饰的方法
2.语法: 访问修饰符 default 返回值类型 方法名称(参数列表) { 方法体; return 返回值 }
3.说明:
A.接口中的默认方法必须通过实例化实例化来进行调用
B.接口的实现类不是强制重写接口中的默认方法
C.接口实现类中方法可以调用接口中的默认方法 调用语法:接口名称.super.方法名()
@FunctionalInterface
public interface XXX {
//默认方法
default int testDefaultMethod(int a,int b){
return a+b;
}
}
1.2 静态方法
1.概念:使用static 进行修饰的方法就是静态方法
2.语法: 访问修饰符 static 返回值类型 方法名称(参数列表) { 方法体 return 返回值 }
3.说明
A.接口中静态方法直接可以通过接口的名称调用
B.实现类不能重写接口中静态方法
@FunctionalInterface
public interface XXX {
//静态方法
static int testStaticMethod(int a,int b){
return a+b;
}
}
1.3 私有方法(JDK9)
1.概念:使用private来进行修饰的方法
2.语法:private 返回值类型 方法名称(参数列表) { 方法体 return 返回值 }
3.说明:
A.在jdk1.9之后才能使用私有的方法
B.实现类不能重写接口中私有的方法
C.接口中普通私有方法可以通过接口中默认的方法来间接访问
D.接口中私有的静态方法可以通过接口中静态方法来间接访问
二、Lambda表达式
2.1 简介
1.本质:就是匿名的接口
2.前提条件:必须是函数型接口 接口中只能有一个抽象方法 可以有其它的方法
3.作用:简化匿名内部类的写法
4.语法: (参数) -> {方法体 }
5.解释: (参数)> 表示的是接口中抽象方法中的参数 -> 没有特殊含义 指向方法体 方法体>接口方法执行的操作
6.简化方式
A.如果表达式只有一个参数 就可以省略数据类型以及括号
B.如果表达式中方法体只有一句话 可以省略大括号以及分号
C.如果表达式中方法体只有一句话是return 就可以省略return以及大括号以及分号
2.2 语法和简化
package com.xxx;
/**
* Lambda表达式
* @author 王则钰 Wyzel
* @date 2022/10/25 15:59
*/
public class Program {
public static void main(String[] args) {
If1 if1 = () -> {
System.out.println("无参数无返回值");
};
if1.test();
If2 if2 = (int a) -> {
System.out.println("一个参数无返回值");
};
if2.test(2);
If3 if3 = (int a, int b) -> {
System.out.println("两个参数无返回值");
};
if3.test(3, 3);
If4 if4 = () -> {
return 4;
};
System.out.println("无参数有返回值" + if4.test());
If5 if5 = (int a) -> {
return a;
};
System.out.println("一个参数有返回值" + if5.test(5));
If6 if6 = (int a,int b) -> {
return a + b;
};
//可通过Lambda简化成
// If6 if6 = Integer::sum;
System.out.println("多个参数有返回值" + if6.test(6, 6));
}
/**
* 无参数无返回值
*/
interface If1 {
void test();
}
/**
* 一个参数无返回值
*/
interface If2 {
void test(int a);
}
/**
* 两个参数无返回值
*/
interface If3 {
void test(int a, int b);
}
/**
* 无参数有返回值
*/
interface If4 {
int test();
}
/**
* 一个参数有返回值
*/
interface If5 {
int test(int a);
}
/**
* 多个参数有返回值
*/
interface If6 {
int test(int a, int b);
}
}
package com.xxx;
/**
* Lambda表达式简化
* 1.参数类型可以省略
* 2.只有1个参数时,可以省略()小括号
* 3.如果方法体只有一条语句,{}花括号可以省略
* 4.如果方法体中唯一的语句时return,那省略{}的同时,return也要省略
* @author 王则钰 Wyzel
* @date 2022/10/25 16:19
* @Description
*/
public class Program2 {
public static void main(String[] args) {
/*
没参数不能省略(),2个以上也不能省略()
如果只有一个语句,{}可以省略
*/
Program.If1 if1 = () ->
System.out.println("无参数无返回值");
if1.test();
/*
单个参数类型可以省略
有1个参数可以省略()
如果只有一个语句,{}可以省略
*/
Program.If2 if2 = a ->
System.out.println("一个参数无返回值");
if2.test(2);
/*
多个参数类型也能省略,一起省略
如果只有一个语句,{}可以省略
*/
Program.If3 if3 = (a, b) ->
System.out.println("两个参数无返回值");
if3.test(3, 3);
/*
只有一条语句,{}和return一起省略
*/
Program.If4 if4 = () -> 4;
System.out.println("无参数有返回值" + if4.test());
/*
省略参数类型
有1个参数可以省略()
只有一条语句,{}和return一起省略
*/
Program.If5 if5 = a -> a;
System.out.println("一个参数有返回值" + if5.test(5));
/*
*/
Program.If6 if6 = Integer::sum;
System.out.println("多个参数有返回值" + if6.test(6, 6));
}
/**
* 无参数无返回值
*/
interface If1 {
void test();
}
/**
* 一个参数无返回值
*/
interface If2 {
void test(int a);
}
/**
* 两个参数无返回值
*/
interface If3 {
void test(int a, int b);
}
/**
* 无参数有返回值
*/
interface If4 {
int test();
}
/**
* 一个参数有返回值
*/
interface If5 {
int test(int a);
}
/**
* 多个参数有返回值
*/
interface If6 {
int test(int a, int b);
}
}