s-day07-常用的函数式接口_Consumer
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
package com.inmind.functional_consumer03;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在jdk1.8之后,java提供了一些常用的函数式接口供开发人员直接使用.
|
||||||
|
|
||||||
|
常用的函数式接口_Consumer
|
||||||
|
Interface Consumer<T>
|
||||||
|
抽象方法:
|
||||||
|
void accept(T t) 对给定的参数执行此操作。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.Consumer表示一个消费者.只接受一个参数,没有返回值.
|
||||||
|
2.什么时候要使用Consumer接口??
|
||||||
|
当我们开发人员想自定义一个函数式接口,并且该接口中要定义一个接收一个参数,没有返回值的方法
|
||||||
|
|
||||||
|
需求:定义出一个使用常用函数式接口的方法,并对字符串进行消费操作(大写打印,小写打印)
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1.大写打印(lambda全写形式)
|
||||||
|
printString((String s)->{
|
||||||
|
System.out.println(s.toUpperCase());
|
||||||
|
},"aBcD");
|
||||||
|
//2.小写打印(lambda省略形式)
|
||||||
|
printString(s-> System.out.println(s.toLowerCase()),"aBcD");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printString(Consumer<String> consumer, String s) {
|
||||||
|
consumer.accept(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.functional_consumer03;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface1 {
|
||||||
|
void accept(String s);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.functional_consumer03;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface2 {
|
||||||
|
void accept(Integer i);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.functional_consumer03;
|
||||||
|
|
||||||
|
import com.inmind.lambda01.Student;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface3 {
|
||||||
|
void accept(Student s);
|
||||||
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package com.inmind.functional_interface02;
|
|
||||||
|
|
||||||
public class Demo05 {
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user