java中的泛型

在写这篇文章之前  就是 要明白泛型在java中的核心概念:告诉编译器想使用什么类型。这样编译器才能帮你处理一些细节。

泛型其实可以应用于类和接口:

1、泛型接口

说到泛型接口不得不说就是生成器,他是专门用来生成对象的类,实际上 他也是工厂设计模式的一种应用。我们直接看代码

/**
 * Created by Administrator on 13-12-22.
 */
public interface Generator<T> {
    T neext();
}

接下里就是需要一些特别的类

/**
 * Created by Administrator on 13-12-22.
 */
public class Coffee {
    public static  long counter = 0;
    private final long id = counter++;

    @Override
    public String toString() {
        return  getClass().getSimpleName()+" "+id;
    }
}


class Latte extends Coffee{}
class Mocha extends Coffee{}

class Americano extends Coffee {
}

接下来就是编写一个类实现generator接口,他能够随机的生成coffee对象

import java.util.Iterator;
import java.util.Random;

/**
 * Created by Administrator on 13-12-22.
 */
public class CoffeeGenerator implements Generator<Coffee> ,Iterable<Coffee> {
    private Class [] types = {
            Americano.class,
            Latte.class,
            Mocha.class,
    };
    private static Random random = new Random(47);
    private int size =  0;
    public CoffeeGenerator(){}

    public CoffeeGenerator(int size) {
        this.size = size;
    }

    @Override
    public Coffee neext() {

        try {
            return (Coffee)types[random.nextInt(types.length)].newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    class CoffeeIterator implements Iterator<Coffee> {
        int count = size;
        public boolean hasNext(){
            return count > 0;
        }

        public Coffee next(){
            count --;
            return CoffeeGenerator.this.neext();
        }

        public void remove(){
            throw  new UnsupportedOperationException();
        }

    }

    @Override
    public Iterator<Coffee> iterator() {
        return new CoffeeIterator();
    }

    public static void main(String[] args) {
        CoffeeGenerator coffees = new CoffeeGenerator();
        for (int i = 0 ;i < 3;i++)
            System.out.println(coffees.neext());
        for (Coffee coffee: new CoffeeGenerator(3))
            System.out.println(coffee);
    }
}

2、泛型方法

define:

public static <T> Collection<T> fill(Collection<T> collection,Generator<T> generator, int n){
            for (int i = 0;i<n;i++){
                collection.add(generator.neext());
            }
            return collection;
        }

好了 我们直接看列子

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by Administrator on 14-1-11.
 */
public class Generators {
//    wo can know how to define a method of type
        public static <T> Collection<T> fill(Collection<T> collection,Generator<T> generator, int n){
            for (int i = 0;i<n;i++){
                collection.add(generator.neext());
            }
            return collection;
        }

    public static void main(String[] args) {
        Collection <Coffee> coffees = fill(new ArrayList<Coffee>(),new CoffeeGenerator(),4);
        for (Coffee coffee : coffees){
            System.out.println(coffee);
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值