1)方法的参数列表有泛型,故方法返回值前要加泛型参数列表;
package chapter15._4._5;
import chapter15._2._1.FiveTuple;
import chapter15._2._1.FourTuple;
import chapter15._2._1.ThreeTuple;
import chapter15._2._1.TwoTuple;
public class Tuple {
public static <A, B> TwoTuple<A, B> tuple(A a, B b) {
return new TwoTuple<A, B>(a, b);
}
public static <A, B, C> ThreeTuple<A, B, C> tuple(A a, B b, C c) {
return new ThreeTuple<A, B, C>(a, b, c);
}
public static <A, B, C, D> FourTuple<A, B, C, D> tuple(A a, B b, C c, D d) {
return new FourTuple<A, B, C, D>(a, b, c, d);
}
public static <A, B, C, D, E> FiveTuple<A, B, C, D, E> tuple(A a, B b, C c, D d, E e) {
return new FiveTuple<A, B, C, D, E>(a, b, c, d, e);
}
}
package chapter15._4._5;
import chapter15._2._1.*;
public class TupleTest2 {
static TwoTuple<String, Integer> f() {
return Tuple.tuple("hi", 47);
}
static TwoTuple f2() {
return Tuple.tuple("hi", 47);
}
static ThreeTuple<Amphibian, String, Integer> g() {
return Tuple.tuple(new Amphibian(), "hi", 47);
}
static FourTuple<Vehicle, Amphibian, String, Integer> h() {
return Tuple.tuple(new Vehicle(), new Amphibian(), "hi", 47);
}
static FiveTuple<Vehicle, Amphibian, String, Integer, Double> k() {
return Tuple.tuple(new Vehicle(), new Amphibian(), "hi", 47, 11.1);
}
public static void main(String[] args) {
TwoTuple<String, Integer> ttsi = f();
System.out.println(ttsi);
System.out.println(f2());
System.out.println(g());
System.out.println(h());
System.out.println(k());
}
}
输出
TwoTuple{first=hi, second=47}
TwoTuple{first=hi, second=47}
ThreeTuple{third=47, first=chapter15._2._1.Amphibian@610455d6, second=hi}
FourTuple{fourth=47, third=hi, first=chapter15._2._1.Vehicle@511d50c0, second=chapter15._2._1.Amphibian@60e53b93}
FiveTuple{fifth=11.1, fourth=47, third=hi, first=chapter15._2._1.Vehicle@5e2de80c, second=chapter15._2._1.Amphibian@1d44bcfa}