用泛型构建复杂容器模型
2015-03-06 22:31
204 查看
泛型的一个重要好处是能够简单而安全地创建复杂的模型,比如组装多层容器时,其泛型就能保障其安全了。
代码例子:
运行结果:
258: Test, price: 400.99861:Test,price:400.99
861: Test, price: 160.99
868: Test, price: 417.99207:Test,price:417.99
207: Test, price: 268.99
551: Test, price: 114.99278:Test,price:114.99
278: Test, price: 804.99
520: Test, price: 554.99140:Test,price:554.99
140: Test, price: 530.99
704: Test, price: 250.99575:Test,price:250.99
575: Test, price: 24.99
674: Test, price: 440.99826:Test,price:440.99
826: Test, price: 484.99
258: Test, price: 400.99861:Test,price:400.99
861: Test, price: 160.99
868: Test, price: 417.99207:Test,price:417.99
207: Test, price: 268.99
551: Test, price: 114.99278:Test,price:114.99
278: Test, price: 804.99
520: Test, price: 554.99140:Test,price:554.99
140: Test, price: 530.99
704: Test, price: 250.99575:Test,price:250.99
575: Test, price: 24.99
674: Test, price: 440.99826:Test,price:440.99
826: Test, price: 484.99
代码例子:
//商品 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:400.99
861: Test, price: 160.99
868: Test, price: 417.99207:Test,price:417.99
207: Test, price: 268.99
551: Test, price: 114.99278:Test,price:114.99
278: Test, price: 804.99
520: Test, price: 554.99140:Test,price:554.99
140: Test, price: 530.99
704: Test, price: 250.99575:Test,price:250.99
575: Test, price: 24.99
674: Test, price: 440.99826:Test,price:440.99
826: Test, price: 484.99
258: Test, price: 400.99861:Test,price:400.99
861: Test, price: 160.99
868: Test, price: 417.99207:Test,price:417.99
207: Test, price: 268.99
551: Test, price: 114.99278:Test,price:114.99
278: Test, price: 804.99
520: Test, price: 554.99140:Test,price:554.99
140: Test, price: 530.99
704: Test, price: 250.99575:Test,price:250.99
575: Test, price: 24.99
674: Test, price: 440.99826:Test,price:440.99
826: Test, price: 484.99
相关文章推荐
- 从头认识java-13.5 利用泛型构建复杂模型
- 从头认识java-13.5 利用泛型构建复杂模型
- 从头认识java-13.5 利用泛型构建复杂模型
- Tensorflow学习精要版III---使用cifar10构建更加复杂的模型
- 无锁并行框架构建复杂消费模型
- (转)Builder模式的误区:将复杂对象的构建进行封装,就是Builder模式了吗?
- STL中Vector容器Find的使用(泛型)
- 使用ASP.NET WEB API构建基于REST风格的服务实战系列教程(一)——使用EF6构建数据库及模型
- Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>
- Linux设备模型(二)上层容器
- kaldi 声学模型构建dict过程
- .Net高级进阶,教你如何构建企业模型数据拦截层,动态控制字段验证
- 3~RocketMQ-集群构建模型详解
- 一类判定性问题的网络流模型的构建
- 用Maven构建Java Web开发环境(Jetty容器)之一
- 构建 复杂系统的TCP/IP网络架构
- 构建可反转排序的泛型字典类(9完)--完善
- Microsoft 用于构建面向服务的应用程序的统一编程模型--Indigo简介 - WCF(WindowsCommunicationFoundation)构建面向服务的分布式应用
- Spring Bean装配之基于Java的容器注解说明——基于泛型的自动装配
- 读书笔记-容器、泛型及反射