美文网首页
java元组实现方式之一

java元组实现方式之一

作者: 建康_木子 | 来源:发表于2019-06-28 16:43 被阅读0次
    二维元组
    public class TwoTuple <A, B>{
        public final A first;
        public final B second;
    
        public TwoTuple(A first, B second) {
            this.first = first;
            this.second = second;
        }
    
        @Override
        public String toString() {
            return "("+first+","+second+")";
        }
    }
    
    测试代码
    public class TupleTest {
        public static void main(String[] args) {
            TwoTuple<Person, Dog> twoTuple = new TwoTuple<>(new Person("zhangsan", "male"), new Dog("dog", "black"));
            System.out.println(twoTuple.toString());
        }
    }
    
    测试结果
    (Person{name='zhangsan', sex='male'},Dog{kind='dog', color='black'})
    
    多维拓展(三维)
    public class ThreeTuple<A,B,C> extends TwoTuple<A,B> {
        public final C third;
    
        public ThreeTuple(A first, B second, C third) {
            super(first, second);
            this.third = third;
        }
    
        @Override
        public String toString() {
            return "("+first+","+second+","+third+")";
        }
    }
    

    相关文章

      网友评论

          本文标题:java元组实现方式之一

          本文链接:https://www.haomeiwen.com/subject/tpjecctx.html