您可以创建一个类来表示存放三个元素的对象,类似于 Pair 类。以下是一个示例:

public class Triple<T, U, V> {
    private T first;
    private U second;
    private V third;

    public Triple(T first, U second, V third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public T getFirst() {
        return first;
    }

    public void setFirst(T first) {
        this.first = first;
    }

    public U getSecond() {
        return second;
    }

    public void setSecond(U second) {
        this.second = second;
    }

    public V getThird() {
        return third;
    }

    public void setThird(V third) {
        this.third = third;
    }
}

使用该类,您可以创建一个存放三个元素的对象,并访问其属性:

Triple<String, Integer, Double> triple = new Triple('Hello', 123, 3.14);
System.out.println(triple.getFirst());  // 输出: Hello
System.out.println(triple.getSecond()); // 输出: 123
System.out.println(triple.getThird());  // 输出: 3.14

triple.setFirst('World');
triple.setSecond(456);
triple.setThird(2.71);
System.out.println(triple.getFirst());  // 输出: World
System.out.println(triple.getSecond()); // 输出: 456
System.out.println(triple.getThird());  // 输出: 2.71

请注意,这里使用了泛型来允许存储不同类型的元素。您可以根据需要调整类型参数。


原文地址: https://www.cveoy.top/t/topic/pUno 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录