在java中一個方法只能返回一個值,無疑要返回多值就要藉助類的封裝,下面主要是利用泛型來構建一個通用的元組,來達到一個方法返回多值的效果java
如:this
public class TwoTuple<A,B> { public final A first; public final B second; public TwoTuple(A a,B b){ this.first = a; this.second = b; } }
TwoTuple的字段加了final的,使其達到值沒法修改的目的。spa
若是要返回三個值就要變成下面形式了:code
public class ThreeTuple<A,B,C> extends TwoTuple<A,B>{ public final C third; public ThreeTuple(A a,B b,C c){ super(a,b); this.third = c; } }
繼續擴展能夠實現多個元組class