双木非林,田下有心。——顾城

首先看这段代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.function.Function;


class Scratch {
static class RoleInfo {
}

public static void main(String[] args) {
// 想让这两个通过
test(Object::hashCode, RoleInfo::hashCode, new RoleInfo());
test(Object::toString, RoleInfo::toString, new RoleInfo());
// 想让这个报错
test(Object::hashCode, RoleInfo::toString, new RoleInfo());
}

public static <T, A, R>
void test(Function<T, R> a,
Function<A, R> b,
T ad) {

}
}

可以看到test方法的第0、1参数分别是Function<T, R>以及Function<A, R>

但这里却一致编译运行通过

image-20220713124721906

最后使用了超类型限定解决了,我们可以看到IntegerString都实现了Comparable接口并限定了其类型:

String

1
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {}

Integer

1
public final class Integer extends Number implements Comparable<Integer> {}

那么我们这里就可以利用这个特性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.function.Function;


class Scratch {
static class RoleInfo {
}

public static void main(String[] args) {
// 想让这两个通过
test(Object::hashCode, RoleInfo::hashCode, new RoleInfo());
test(Object::toString, RoleInfo::toString, new RoleInfo());
// 想让这个报错
test(Object::hashCode, RoleInfo::toString, new RoleInfo());
}

public static <T, A, R extends Comparable<R>>
void test(Function<T, R> a,
Function<A, R> b,
T ad) {

}
}

达成效果:

image-20220713124631978