用泛型构建复杂容器模型

泛型的一个重要好处是能够简单而安全地创建复杂的模型,比如组装多层容器时,其泛型就能保障其安全了。
代码例子:

//商品
class Product
{
    private final int id;
    private String description;
    private double price;
    public Product(int id, String description, double price)
    {
        super();
        this.id = id;
        this.description = description;
        this.price = price;
        System.out.println(toString());
    }
    @Override
    public String toString()
    {       
        return id+": "+description+", price: $"+price;
    }
    public void priceChange(double change)
    {
        price+=change;
    }
    //这里用静初始化不单单为方便调用,更重要的是共用这个对象减少内存开销
    public static Generator<Product> generator=new Generator<Product>()
    {
        private Random random=new Random(47);
        @Override
        public Product next()
        {
            return new Product(random.nextInt(1000),"Test",Math.round(random.nextDouble()*1000.0)+0.99);
        }
    };
}
//货架,让其摆设多个商品
@SuppressWarnings("serial")
class Shelf extends ArrayList<Product>
{
    public Shelf(int nProducts)
    {
        //生产器工具类填充这个货架,其元素为多个Product商品
         Generators.fill(this, Product.generator, nProducts);
    }
}
 //走廊摆设多个货架
@SuppressWarnings("serial")
class Aisle extends ArrayList<Shelf>
{
    public Aisle(int nShelves,int nProducts)
    {
        //添加多个货架
        for(int i=0;i<nShelves;i++)
            add(new Shelf(nProducts));
    }
}
//商店摆设多个走量
@SuppressWarnings("serial")
public class Store extends ArrayList<Aisle>
{
    public Store(int nAisles,int nShelves,int nProducts)
    {
         //添加多个走廊
        for(int i=0;i<nAisles;i++)
            add(new Aisle(nShelves, nProducts));
    }
    @Override
    public String toString()
    {
        //用字符容器装其内容
        StringBuilder builder=new StringBuilder();
        for(Aisle a:this)
            for(Shelf s:a)
                for(Product p:s)
                {
                    builder.append(p);
                    builder.append("\n");
                }                   
        return builder.toString();
    }
    public static void main(String[] args)
    {
        System.out.println(new Store(2, 3, 2));
    }
}

运行结果:
258: Test, price: 400.99861:Test,price: 160.99
868: Test, price: 417.99207:Test,price: 268.99
551: Test, price: 114.99278:Test,price: 804.99
520: Test, price: 554.99140:Test,price: 530.99
704: Test, price: 250.99575:Test,price: 24.99
674: Test, price: 440.99826:Test,price: 484.99
258: Test, price: 400.99861:Test,price: 160.99
868: Test, price: 417.99207:Test,price: 268.99
551: Test, price: 114.99278:Test,price: 804.99
520: Test, price: 554.99140:Test,price: 530.99
704: Test, price: 250.99575:Test,price: 24.99
674: Test, price: 440.99826:Test,price: 484.99

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值