双木非林,田下有心。——顾城
首先看这段代码:
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>
但这里却一致编译运行通过
最后使用了超类型限定解决了,我们可以看到Integer
和String
都实现了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) {
} }
|
达成效果: