forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnboundMethodReference.java
More file actions
48 lines (40 loc) · 1.14 KB
/
Copy pathUnboundMethodReference.java
File metadata and controls
48 lines (40 loc) · 1.14 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
// functional/UnboundMethodReference.java
// (c)2020 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Method reference without an object
interface MakeString {
String make();
}
class X {
String f() { return "X::f()"; }
}
interface MakeString2 {
String make();
}
class X2 {
String f() {
System.err.println("success!");
return "success!";
}
}
interface TransformX {
String transform(X x);
}
public class UnboundMethodReference {
public static void main(String[] args) {
X2 x2 = new X2();
// MakeString2 c = X2::f; 没有实例
MakeString2 c = x2::f;
c.make();
// MakeString ms = X::f; // [1] self-note: 这个是大写的X,不是上面例子的小写d了,没有实例,因此其实需要一个实例用作参数.
TransformX sp = X::f; //总之静态方法引用 X::f 写法是需要多一个参数,要么就用非静态的方法引用 x2::f
X x3 = new X();
System.err.println(sp.transform(x3)); // [2]
System.err.println(x3.f()); // Same effect
}
}
/* Output:
X::f()
X::f()
*/