class E{}
class F{}
class TwoTuple<A,B> {
public final A first;
public final B second;
public TwoTuple(A a,B b){
first=a;
second= b;
}
public String toString(){
return "("+first+"."+second+")";
}
}
class ThreeTuple<A,B,C>{
public final A first;
public final B second;
public final C third;
public ThreeTuple(A a,B b,C c){
first=a;
second=b;
third=c;
}
public String toString(){
return "("+first+"."+second+"."+third+")";
}
}
class FourTuple<A,B,C,D>{
public final A first;
public final B second;
public final C third;
public final D four;
public FourTuple(A a,B b,C c,D d){
first=a;
second=b;
third=c;
four=d;
}
public String toString(){
return "("+first+"."+second+"."+"."+third+"."+four+")";
}
}
public class Tuple{
static TwoTuple<String,Integer> f(){
return new TwoTuple<String,Integer>("hi",47);
}
static ThreeTuple<String,Integer,E> g(){
return new ThreeTuple("how",48,new E());
}
static FourTuple<F,E,String,Integer> h(){
return new FourTuple(new F(),new E(),"are",54);
}
public static void main(String[] args){
TwoTuple<String,Integer> myTwoTuple=f();//其中不能写成TwoTuple myTwoTuple=f();否则会报为初始化
System.out.println(myTwoTuple);
System.out.println(h());
System.out.println(g());
}
}
当然上面的几个类也可以用继承机制来简化代码量。