package com.hrf.newFeature01;
import java.util.function.Consumer;
public class ConsumerInterface {
public static void main(String[] args) {
Person person = new Person("张三", 12);
Consumer<Person> consumer = x -> System.out.println(x.getName());
show("参数",x->System.out.println(x));
}
public static void show(String param, Consumer<String> consumer) {
consumer.accept(param);
}
}
class Person {
String name;
Integer age;
public Person() {
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
package com.hrf.newFeature01;
import java.util.function.Function;
public class FunctionalInterface {
public static void main(String[] args) {
Function<String, String> function = x ->"获取类型:" + x;
String s =function.apply("函数式接口");
System.out.println(s);
String str = show("接收的数据",(x)->x);
System.out.println(str);
}
public static String show(String param,Function<String, String> function) {
return function.apply(param);
}
}
package com.hrf.newFeature01;
import java.util.function.Predicate;
public class PredicateInterface {
public static void main(String[] args) {
System.out.println(show("hrf",x -> {
if(x.equals("a")) return true;
else return false;
}));
}
public static boolean show(String param,Predicate<String> predicate) {
return predicate.test(param);
}
}
package com.hrf.newFeature01;
import java.util.function.Supplier;
public class SupplierInterface {
public static void main(String[] args) {
Supplier<Person01> supplier = () ->new Person01("李思思", 13);
System.out.println(supplier.get());
show("获取数据:", ()->(int)(Math.random()*100));
}
public static void show(String param,Supplier<Integer> supplier) {
System.out.println(supplier.get());
}
}
class Person01{
String name;
Integer age;
public Person01() {
}
public Person01(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}