import java.util.function.Consumer;
public class Person {
private String name;
private int age;
private int height;
private int weight;
public Person(String name, int age, int height, int weight) {
super();
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", height=" + height + ", weight=" + weight + "]";
}
public class Case {
public void dealPerson(Person[] per, int age) {
for (Person p : per) {
if (p.getAge() == age) {
System.out.println(p);
}
}
}
public void dealPerson2(Person[] per, int minAge, int maxAge) {
for (Person p : per) {
int age = p.getAge();
if (age >= minAge && age <= maxAge) {
System.out.println(p);
}
}
}
public void dealPerson3(Person[] per, Rule r) {
for (Person p : per) {
if (r.test(p)) {
System.out.println(p);
}
}
}
public void dealPerson4(Person[] per, Predicate<Person> pre) {
for (Person p : per) {
if (pre.test(p)) {
System.out.println(p);
}
}
}
public void dealPerson5(Person[] per, Predicate<Person> pre, Operate op) {
for (Person p : per) {
if (pre.test(p)) {
op.accept(p);
}
}
}
public void dealPerson6(Person[] per, Predicate<Person> pre, Consumer<Person> c) {
for (Person p : per) {
if (pre.test(p)) {
c.accept(p);
}
}
}
}
@FunctionalInterface
interface Rule {
boolean test(Person p);
}
@FunctionalInterface
interface Operate {
void accept(Person p);
}
public class Client {
public static void main(String[] args) {
Case c = new Case();
Person[] p = new Person[3];
p[0] = new Person("人1", 10, 100, 120);
p[1] = new Person("人2", 20, 150, 130);
p[2] = new Person("人3", 30, 160, 140);
// c.dealPerson(p, 20);
// c.dealPerson2(p, 15, 40);
// 显式声明一个类,指定过滤条件
// c.dealPerson3(p, new MyRule());
// 使用匿名类,指定过滤条件
// c.dealPerson3(p, new Rule() {
// @Override
// public boolean test(Person p) {
// return p.getAge() > 15 && p.getHeight() > 155;
// }
// });
// 使用Lambda表达式,指定过滤条件
// c.dealPerson3(p, per -> per.getAge() > 15 && per.getHeight() > 155);
// c.dealPerson4(p, per -> per.getAge() > 15 && per.getHeight() > 155);
// c.dealPerson5(p, per -> per.getAge() > 10, new MyOperate());
// c.dealPerson5(p, per -> per.getAge() > 10, new Operate() {
// @Override
// public void accept(Person p) {
// System.out.println(p.getName());
// }
// });
c.dealPerson5(p, per -> per.getAge() > 10, per -> System.out.println(per.getName()));
c.dealPerson5(p, per -> per.getHeight() > 120, per -> System.out.println(per.getAge()));
c.dealPerson6(p, per -> per.getHeight() > 120, per -> System.out.println(per.getAge()));
}
}
class MyRule implements Rule {
@Override
public boolean test(Person p) {
// if (p.getAge() > 15 && p.getHeight() > 155) {
// return true;
// } else {
// return false;
// }
return p.getAge() > 15 && p.getHeight() > 155;
}
}
class MyOperate implements Operate {
@Override
public void accept(Person p) {
System.out.println(p.getName());
}
}
java的lambda表达式、匿名类,Predicate接口,Consumer接口的应用
最新推荐文章于 2024-07-05 16:51:15 发布