当前位置:网站首页>函数式接口和方法引用
函数式接口和方法引用
2022-07-02 07:35:00 【Cold Snowflakes】
Lambda表达式描述的是,接口中那个抽象方法要做的事情。
实际Lambda本质是,实现了那个接口的一个类的对象。
接口的组成
常量:默认是 public static final
方法:默认是 public abstract
在JAVA8中,增加了默认方法(public default),静态方法(public static)
在JAVA9中,增加了私有方法
For Instance
public interface eatable {
// 抽象方法 实现类 必须进行实现
void funAbstract();
// 默认方法 和 静态方法 都需要方法体
/** * 默认方法: 不强制实现类必须重写, 实现类可以直接使用 * 主要用于接口的升级,而不破坏现有的代码 */
default void funDefault(){
System.out.println("默认方法");
}
/** * 接口中的静态方法 只能被接口调用 */
static void funStatic(){
System.out.println("静态方法");
}
}
public static void main(String[] args) {
// 实现 抽象方法 并调用 默认方法
((eatable) () -> System.out.println("实现抽象方法")).funDefault();
// 实现 抽象方法 并调用 抽象方法
((eatable) () -> System.out.println("实现抽象方法")).funAbstract();
// 接口中的静态方法 只能被 接口调用, 不能被实现类调用
eatable.funStatic(); // OK
((eatable) () -> System.out.println("实现抽象方法")).funStatic(); // Error
}
方法引用(使用已有的方法逻辑去处理接口中的那个抽象方法)
引用 类中的 静态方法
// 抽象方法的参数 全部传递给 被引用的静态方法
public static void main(String[] args) {
((eatable) s -> Integer.parseInt(s)) // lambda 形式
.toInt("2349645");
((eatable) Integer::parseInt) // 类名::静态方法
.toInt("23");
}
interface eatable {
void toInt(String s);
}
引用 类中的 非静态方法
// 抽象方法的参数 全部传递给 被引用的非静态方法
public static void main(String[] args) {
((eatable)s -> System.out.println(s.toUpperCase()))
.toUpper("fdf");
((eatable)new linshi()::printString)
.toUpper("fsdf"); // 借助对象 , 引用类中的非静态方法
}
class linshi {
public void printString(String s){
System.out.println(s.toUpperCase());
}
}
interface eatable {
void toUpper(String s);
}
也可以这样:
但注意:接口中的那个抽象方法 第一个参数的参数类型 得是 linshi,后面的参数传给了 printString 方法。
public static void main(String[] args) {
((eatable)(s,a) -> System.out.println(a.toUpperCase()))
.toUpper(new linshi(),"fgwerg");
/** * 使用这种 类名::非静态方法 形式的时候 * * 注意 接口中的那个抽象方法 第一个参数的参数类型 得是 linshi * 之后的参数 都传给 printString 方法 */
((eatable)linshi::printString)
.toUpper(new linshi(),"grg");
}
class linshi {
public void printString(String s){
System.out.println(s.toUpperCase());
}
}
interface eatable {
// 第一个参数 作为调用者
void toUpper(linshi a,String s); // 第二个参数 给到 printString 方法去作为参数
}
函数式接口
有且仅有一个抽象方法的接口,函数式接口就是 lambda 表达式的使用前提。
可以使用 @FunctionalInterface 进行标记 函数式接口。
使用函数式接口 作为参数时,可以传递过去一个 lambda 表达式。
public class lambdademo {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<>();
array.add("ccc");
array.add("aa");
array.add("b");
array.add("dddd");
System.out.println("排序前: " + array);
// Collections.sort(array); // 自然排序
Collections.sort(array,getComparator()); // 比较器排序
System.out.println("排序后: " + array);
}
private static Comparator<String> getComparator(){
// 匿名内部类方式
// return new Comparator<String>() {
// @Override
// public int compare(String s1, String s2) {
// return s1.length() - s2.length();
// }
// };
// lambda方式, 因为返回值是 函数式接口
return (s1,s2) -> s1.length() - s2.length();
}
}
常用函数式接口
Supplier接口
public static void main(String[] args) {
// 传一个 lambda ,实际上是 定义 抽象方法Supplier.get的逻辑
String string = getString(() -> "林青霞");
System.out.println(string);
}
// 这里设置接口泛型 为 String, 表示 get 方法 返回类型是 String
private static String getString(Supplier<String> sup){
return sup.get();
}
Consumer接口
public static void main(String[] args) {
// 第一个参数是 传过去的数据
// 第二个参数是 定义 抽象方法 accept的逻辑
getString("随便吧",System.out::print);
}
// 泛型 String 指定了 accept 的参数类型
private static void getString(String s, Consumer<String> cn){
// cn.accept(s);
Consumer<String> t = a -> System.out.print(a.length() + 1);
cn.andThen(t).accept(s);
// cn先执行一次 accept(s)
// t 再执行一次 accept(s)
}
Predicate接口
常用于判断参数是否满足指定条件
boolean test(T t) // 对给定参数进行判断, 判断逻辑由 lambda 提供
default Predicate<T> negate()
default Predicate<T> and(Predicate other)
default Predicate<T> or(Predicate other)
public static void main(String[] args) {
boolean res = checkString("fewef", s -> s.length() > 8);
System.out.print(res);
}
private static boolean checkString(String s, Predicate<String> t){
// return t.test(s);
// return t.negate().test(s); // 非
// return t.and(a -> a.startsWith("fy")).test(s); // 与
return t.or(a -> a.startsWith("fy")).test(s); // 或
}
边栏推荐
- static 函数中的静态变量
- V2X-Sim数据集(上海交大&纽约大学)
- How does the whole network display IP ownership?
- 二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
- 华为AppLinking中统一链接的创建和使用
- Creation and use of unified links in Huawei applinking
- 2. Hacking lab script off [detailed writeup]
- Implement custom drawer component in quick application
- 2022-06-17
- 【深入浅出玩转FPGA学习2----设计技巧(基本语法)】
猜你喜欢

2022-06-17
![[AI application] Hikvision ivms-4200 software installation](/img/4e/1640e3cafac13ce4d39520195c3217.png)
[AI application] Hikvision ivms-4200 software installation

二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)

Special topic of binary tree -- acwing 1589 Building binary search tree

二叉树专题--AcWing 1589. 构建二叉搜索树

Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)

Overview of integrated learning

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

Flink two Open, implement Batch Lookup join (attached source)

Common methods of JS array
随机推荐
华为快应用中如何实现同时传递事件对象和自定义参数
最详细MySql安装教程
JVM garbage collector
Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
主键策略问题
【深入浅出玩转FPGA学习3-----基本语法】
使用华为性能管理服务,按需配置采样率
2022-06-17
JSP webshell免杀——JSP的基础
华为联机对战服务玩家掉线重连案例总结
TIPC Getting Started6
【ARK UI】HarmonyOS ETS的启动页的实现
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
Iii. Système de démarrage et d'horloge à puce
P1055 [NOIP2008 普及组] ISBN 号码
Learn open62541 -- [66] UA_ Generation method of string
How to use ide to automatically sign and debug Hongmeng application
Special topic of binary tree -- Logu p1229 traversal problem (the number of traversals in the middle order is calculated when the pre and post order traversals of the multiplication principle are know
全网显示 IP 归属地,是怎么实现的?
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)