//代码示例 这是一个典型的二维元组
public class TwoTuple<A, B> {
public final A first;
public final B second;
public TwoTuple(A first, B second) {
this.first = first;
this.second = second;
}
@Override
public String toString() {
return "TwoTuple [first=" + first + ", second=" + second + "]";
}
}
//代码示例 这是使用元组的代码
public class TupleTest {
public static TwoTuple<String,Double> speak(){
return new TwoTuple<String, Double>("1232", 1.23);
}
public static void main(String[] args) {
System.out.println(speak());
}
}
元组使用总结:元组定义好之后,将元组作为方法的返回值,该方法的return语句则可以返回该元组,元组中的对象就可以一次性被该方法返回。
如果我们需要使用更多维的元组,就利用Java的继承机制。下面看一个继承自二维元组的三维元组:
//代码示例
public class ThreeTuple<A, B, C> extends TwoTuple<A, B> {
public final C third;
public TwoTuple(A first, B second, C third) {
super(first,second);
this.third = third;
}
}
原文:https://www.cnblogs.com/hackerstd/p/12651721.html