Java怎么使用元组数据
1、建立一个类:OneTuple
public class OneTuple<A> {
private A one;
public OneTuple(A a){
one = a;
}
public A getOneValue() {
return one;
}
}
这个类表示返回的是一个元素的元组。
2、再新建一个类:TwoTuple
public class TwoTuple<A,B> extends OneTuple<A> {
private B two;
public TwoTuple(A a,B b) {
super(a);
this.two = b;
}
public B getTwoValue() {
return two;
}
}
这个表示返回 两个元素的元组,如此类推,可以通过继承达到多元组的目的
3、测试:
public interface Test {
public static void main(String[] args) {
OneTuple<String> one = new OneTuple<>("返回一个属性的元组");
TwoTuple<Integer, String> two = new TwoTuple<Integer, String>(18, "十八");
System.out.println("-----------------------");
System.out.println(one.getOneValue());
System.out.println("-----------------------");
System.out.println(two.getOneValue() + " - " + two.getTwoValue());
}
}
结果如图所示: