java泛型元祖_泛型的应用——一个元组类库

一个元组类库

仅一次方法调用就能返回多个对象,作为我们需要的一个功能呢,这在动态语言上非常常见。可是Java这种静态类型的语言,return语句只允许返回单个对象。而解决方法往往是通过创建一个类,用它来持有其所有返回的多个对象。

当然,在每次需要的时候,可以专门通过创建一个类来工作,但是有了泛型以后,我们就能够一次性的解决这个问题,并且不用在这个问题上浪费时间。同时,我们在编译器就能确保类型安全。

元组的概念

元组及将一组对象直接打包,并且储存于其中的一个单一对象。这个容器元素允许读取其中元素,但是不允许向其中存放新的对象。(这个概念也被称为数据传递对象或者信使)。

通常,元组可以具有任意的长度,同时,元组中的对象可以是任意不同的类型。不过我们希望可以为每一个对象指明其类型,并且从容器中读取出来的时候,能够得到正确的类型。

泛型中要处理不同长度的问题,我们需要创建多个元组来实现。下面是一个2维元组,它能够持有两个对象。

public class TwoTuple {

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 "(" + getTupleString() + ")";

}

protected String getTupleString() {

return first + ", " + second;

}

}

TwoTuple中通过构造器捕获了要存储的对象,而toString是一个便利的函数,用来显示列表中的值,并隐含地保持了其中元素的次序。

为了安全性考虑,上面的first和second字段都声明了final对象,这意味者着两个变量赋值后无法被改变,在不用实现get和set方法的情况同时保证了安全。

我们可以利用继承机制实现更长的数组, 增加类型参数是件很简单的事情。

public class ThreeTuple extends TwoTuple{

public final C third;

public ThreeTuple(A first, B second, C third) {

super(first, second);

this.third = third;

}

@Override

protected String getTupleString() {

return super.getTupleString() + ", " + third;

}

}

public class FourTuple extends ThreeTuple{

public D fourth;

public FourTuple(A first, B second, C third, D fourth) {

super(first, second, third);

this.fourth = fourth;

}

@Override

protected String getTupleString() {

return super.getTupleString() + ", " + fourth;

}

}

public class FiveTuple extends FourTuple{

public E fifth;

public FiveTuple(A first, B second, C third, D fourth, E fifth) {

super(first, second, third, fourth);

this.fifth = fifth;

}

@Override

protected String getTupleString() {

return super.getTupleString() + ", " + fifth;

}

}

而为了使用元组,你只需定义一个合适长度的数组,并将其作为方法的返回值,然后在return语句中创建该元组并返回。

public class TupleTest {

static TwoTuple f(){

return new TwoTuple<>("hi",47);

}

static ThreeTuple g(){

return new ThreeTuple<>(new Amphibian(),"h1",47);

}

static FourTuple h(){

return new FourTuple<>(new Vehicle(),new Amphibian(),"h1",47);

}

static FiveTuple k(){

return new FiveTuple<>(new Vehicle(),new Amphibian(),"h1",47,11.1);

}

public static void main(String[] args) {

TwoTuple ttsi = f();

System.out.println("ttsi = " + ttsi);

System.out.println("g() = " + g());

System.out.println("h() = " + h());

System.out.println("k() = " + k());

}

}

class Amphibian{}

class Vehicle{}

//Outputs

ttsi = (hi, 47)

g() = (com.daidaijie.generices.tuple.Amphibian@1540e19d, h1, 47)

h() = (com.daidaijie.generices.tuple.Vehicle@677327b6, com.daidaijie.generices.tuple.Amphibian@14ae5a5, h1, 47)

k() = (com.daidaijie.generices.tuple.Vehicle@7f31245a, com.daidaijie.generices.tuple.Amphibian@6d6f6e28, h1, 47, 11.1)

由于有了泛型,所以可以很容易的创建元组并令其返回一组任意类型的对象。

虽然如此,但是编写任意多类型的元组仍需要创建许多泛型类,这里需要根据情况来分析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值