`
TRAMP_ZZY
  • 浏览: 132548 次
社区版块
存档分类
最新评论

Java 实现Tuple

    博客分类:
  • Java
阅读更多

在一次方法调用就能返回多个对象,实现return 的这个功能,采用泛型实现Tuple


public class Tuple<A, B> {
    public final A first;
    public final B second;

    public Tuple(A first, B second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public String toString() {
        return "Tuple{" +
                "first=" + first +
                ", second=" + second +
                '}';
    }

    public static void main(String[] args) {
        Tuple<String, Integer> tuple = new Tuple<>("zhang", 12);
        System.out.println(tuple.first);
        System.out.println(tuple.second);
    }
}

class ThreeTuple<A, B, C> extends Tuple<A, B> {
    public final C third;

    public ThreeTuple(A first, B second, C third) {
        super(first, second);
        this.third = third;
    }
}

class FourTuple<A, B, C, D> extends ThreeTuple<A, B, C> {
    public final D fourth;

    public FourTuple(A first, B second, C third, D fourth) {
        super(first, second, third);
        this.fourth = fourth;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics