java模型给泛型_从头认识java-13.5 利用泛型构建复杂模型

这一章节我们来展示一下如何利用泛型构建复杂模型?

1.元组列表

我们之前已经说过元组是一个复杂的模型,能够返回多对象。

package com.ray.ch11;

import java.util.ArrayList;

public class Test {

public ArrayList> test() {

ArrayList> list = new ArrayList>();

for (int i = 0; i < 10; i++) {

list.add(new Tuple(new A(), new B(), new C()));

}

return list;

}

public static void main(String[] args) {

new Test().test();

}

}

class A {

}

class B {

}

class C {

}

@SuppressWarnings("hiding")

class Tuple {

public final A a;

public final B b;

public final C c;

public Tuple(A a, B b, C c) {

this.a = a;

this.b = b;

this.c = c;

}

}

上面的代码我们通过元组来实现一个比較复杂的模型。

我们以下再引用另外一个样例。一个商店。

2.商店

这个商店由办公区、前台、销售区组成,并且销售区由若干货架组成,货架上面又须要放置多种货物。

package com.ray.ch11;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Random;

public class Store extends ArrayList {

private Office office = new Office();

private CheckOut checkOut = new CheckOut();

public Store(int saleZoneNum, int shelfNum, int produceNum) {

for (int i = 0; i < saleZoneNum; i++) {

add(new SaleZone(shelfNum, produceNum));

}

}

public static void main(String[] args) {

new Store(1, 2, 5);

}

}

class Product {

private int id = 0;

private String name = "";

private double price = 0.0;

public Product(int id, String name, double price) {

this.id = id;

this.name = name;

this.price = price;

System.out.println(toString());

}

public static Generator generator = new Generator() {

@Override

public Product next() {

Random random = new Random();

int id = random.nextInt();

return new Product(id, "test-" + id, random.nextDouble());

}

};

@Override

public String toString() {

return "produce id: " + id + " name: " + name + " price: " + price;

}

}

interface Generator {

public T next();

}

class Generators {

public static Collection fill(Collection collection,

Generator generator, int num) {

for (int i = 0; i < num; i++) {

collection.add(generator.next());

}

return collection;

}

}

class Shelf extends ArrayList {

/**

*

*/

private static final long serialVersionUID = 1L;

public Shelf(int produceNum) {

Generators.fill(this, Product.generator, produceNum);

}

}

class SaleZone extends ArrayList {

/**

*

*/

private static final long serialVersionUID = 1L;

public SaleZone(int shelfNum, int produceNum) {

for (int i = 0; i < shelfNum; i++) {

add(new Shelf(produceNum));

}

}

}

class Office {

}

class CheckOut {

}

大家可能理解上面的代码会比較复杂一点,我解释一下:

1.第一个难度在于生成器,假设读了前面章节或许会简单一点。

事实上这里使用生成器,主要是为了抽象出一个比較通用的生成器。假设是一般的代码,我们能够在product里面直接返回一个produceList,这种代码看上去或许会好非常多。

2.Generators,主要是抽象出往容器填充数据的通用性代码。

3.里面有几个类都直接继承了ArrayList,这里是为了在构造器的时候就能够直接调用add方法,不用在构造一次ArrayList。假设依照寻常的习惯,或许我们会自己建立一个ArrayList,然后往里面填充数据就算了

4.使用匿名内部类在product里面创建生成器。

总结:这一章节主要是展示一下如何利用泛型构建复杂模型。

这一章节就到这里,谢谢。

-----------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值