forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
61 lines (47 loc) · 1.46 KB
/
Person.java
File metadata and controls
61 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @program JavaBooks
* @description: 方法和函数式引用
* @author: mf
* @create: 2020/01/23 16:36
*/
package com.java8;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
public class Person {
String name;
Integer age;
public Person() {
}
public Person(String name) {
this.name = name;
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) {
// 当只有1个参数时
Supplier<Person> supplier = Person::new; // 构造函数引用指向默认的Apple()构造函数
Person person = supplier.get(); // new 一个对象
System.out.println(person);
Supplier<Person> supplier1 = () -> new Person(); // 和上面等价
Person person1 = supplier.get();
System.out.println(person1);
// 当只有一个参数时
Function<String, Person> function = Person::new;
Person person2 = function.apply("mai");
System.out.println(person2);
// 当有两个参数时
BiFunction<String, Integer, Person> function1 = Person::new;
Person person3 = function1.apply("feng", 25);
System.out.println(person3);
}
}