进阶day06-常用的函数式接口_Consumer

This commit is contained in:
2026-03-10 16:18:37 +08:00
parent 7f4b0cd01c
commit 016426e963
4 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package com.inmind.functional_interface02;
import java.util.function.Consumer;
/*
在jdk1.8之后,java提供了一些常用的函数式接口供开发人员直接使用.
9.常用的函数式接口_Consumer
Interface Consumer<T>
抽象方法:
void accept(T t) 对给定的参数执行此操作。
注意:
1.Consumer表示一个消费者.只接受一个参数,没有返回值.
2.什么时候要使用Consumer接口??
当我们开发人员想自定义一个函数式接口,并且该接口中要定义一个接收一个参数,没有返回值的方法
需求:定义出一个使用常用函数式接口的方法,并对字符串进行消费操作(大写打印,小写打印)
*/
public class Demo05 {
public static void main(String[] args) {
// print((String s)->{System.out.println(s.toLowerCase());},"aBcD");
print(s->System.out.println(s.toLowerCase()),"aBcD");
}
//定义出一个使用常用函数式接口的方法
public static void print(Consumer<String> consumer,String str) {
consumer.accept(str);
}
}

View File

@@ -0,0 +1,6 @@
package com.inmind.functional_interface02;
@FunctionalInterface
public interface MyInterface1 {
void method1(int i);
}

View File

@@ -0,0 +1,6 @@
package com.inmind.functional_interface02;
@FunctionalInterface
public interface MyInterface2 {
void method2(int a);
}

View File

@@ -0,0 +1,6 @@
package com.inmind.functional_interface02;
@FunctionalInterface
public interface MyInterface3 {
void method3(int c);
}