00后,话不多说:
package First;
import java.util.function.*;
// 定义一个Human类
class Human {
private String name;
private int age;
private double height;
private double weight;
// 构造函数
public Human(String name, int age, double height, double weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
// 获取名字
public String getName() {
return name;
}
// 获取年龄
public int getAge() {
return age;
}
// 获取身高
public double getHeight() {
return height;
}
// 获取体重
public double getWeight() {
return weight;
}
// 设置年龄
public void setAge(int age) {
this.age = age;
}
// 设置身高
public void setHeight(double height) {
this.height = height;
}
// 设置体重
public void setWeight(double weight) {
this.weight = weight;
}
// 计算BMI指数
public double calculateBMI() {
return weight / (height * height);
}
// 输出Human对象的基本信息
public void printInfo() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("身高:" + height);
System.out.println("体重:" + weight);
System.out.println("BMI指数:" + calculateBMI());
}
}
public class ComplexUnaryEventExample {
public static void main(String[] args) {
// 创建一个Human对象
Human human = new Human("张三", 30, 1.75, 70);
// 定义一个复杂的单目事件,将Human对象的年龄增加10岁,同时计算BMI指数
UnaryOperator<Human> event = p -> {
p.setAge(p.getAge() + 10);
double newBMI = p.calculateBMI();
p.setHeight(p.getHeight() + 0.1 * (newBMI - 25.0));
p.setWeight(p.getWeight() + 2.5);
return p;
};
// 调用单目事件并输出Human对象的基本信息
Human updatedHuman = event.apply(human);
updatedHuman.printInfo();
}
}
可以自己选择修改数字