---
title: "Java 实现Tuple"
author: "TRAMP_ZZY"
date: "2015-12-07T14:08:00Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java-tuple"
draft: false
lastmod: "2015-12-07T14:14:15Z"
id: 2262294
url: "https://tramp-zzy.iteye.com/blog/2262294"
---

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

```java

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;
    }
}

```