用java写一个Lambda表达式的示例。提供三个单函数接口:Eatable、Flyable、Addable,编写测试类,运用Lambda表达式进行测试。
时间: 2024-06-12 13:05:48 浏览: 44
```java
// Eatable.java
@FunctionalInterface
public interface Eatable {
void eat();
}
// Flyable.java
@FunctionalInterface
public interface Flyable {
void fly(int height);
}
// Addable.java
@FunctionalInterface
public interface Addable {
int add(int a, int b);
}
// TestLambda.java
public class TestLambda {
public static void main(String[] args) {
// 使用Lambda表达式创建Eatable对象
Eatable eatable = () -> System.out.println("吃饭了!");
eatable.eat();
// 使用Lambda表达式创建Flyable对象
Flyable flyable = height -> System.out.println("飞行高度为:" + height + "米");
flyable.fly(1000);
// 使用Lambda表达式创建Addable对象
Addable addable = (a, b) -> a + b;
int result = addable.add(3, 5);
System.out.println("3 + 5 = " + result);
}
}
```
阅读全文