This commit is contained in:
wu 2024-10-24 11:59:13 +08:00
parent 9558107356
commit 7f461fc7de

View File

@ -266,15 +266,11 @@ System.out.println(append.convert("Hello", "Lambda")); // 输出 "Hello, Lambda"
```java ```java
// 假设我们有一个简单的类 // 假设我们有一个简单的类
class Example { class Example {
private final int value; public Example() {
public Example(int value) {
this.value = value;
} }
@Override public String doSomething() {
public String toString() { return "Example do something“;
return "Example{" + "value=" + value + '}';
} }
} }
@ -286,13 +282,11 @@ interface ExampleSupplier {
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// 使用lambda表达式和构造函数引用创建ExampleSupplier实例 // 使用lambda表达式和构造函数引用创建ExampleSupplier实例
ExampleSupplier supplier = () -> new Example(42); ExampleSupplier supplier = Example::new;
// 使用ExampleSupplier实例来创建Example对象 // 使用ExampleSupplier实例来创建Example对象
Example example = supplier.get(); Example example = supplier.get();
example.doSomething();
// 输出结果
System.out.println(example);
} }
} }
``` ```