public static void main(String[] args) { String[] arr = asArray("one", "two", "three"); System.out.println(Arrays.toString(arr)); // ClassCastException: Object[] firstTwo = pickTwo("one", "two", "three"); System.out.println(firstTwo[0].getClass().getName()); } static <K> K[] pickTwo(K k1, K k2, K k3) { return asArray(k1, k2); } static <T> T[] asArray(T... objs) { return objs; }
输出的结果:
[one, two, three] java.lang.String
Sign in to make a reply
Momo
输出的结果: