这一章节我们来讨论一下泛型接口的使用。
我们以生成器为例子(generator),生成器是工厂方法的一种运用,主要用来创建对象,一般使用工厂方法都是需要输入参数以便取得不同的对象,但是生成器是生成一系列的对象。
代码:
package com.ray.ch13; import java.util.Iterator; import java.util.Random; public class Test implements Generator<Father> { private Class<?>[] classes = { Sub1.class, Sub2.class, Sub3.class }; private Random random = new Random(); @Override public Father next() { Father father = null; try { father = (Father) classes[random.nextInt(3)].newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return father; } public static void main(String[] args) { Test test = new Test(); for (int i = 0; i < 5; i++) { System.out.println(test.next()); } } } interface Generator<T> { T next(); } class Father { private static int counter = 0; private final int id = counter++; @Override public String toString() { return "name:" + getClass().getSimpleName() + " id:" + id; } } class Sub1 extends Father { } class Sub2 extends Father { } class Sub3 extends Father { }
输出:
name:Sub3 id:0
name:Sub1 id:1
name:Sub3 id:2
name:Sub3 id:3
name:Sub3 id:4
上面的代码通过生成器生成了5个Father 的自对象Sub,在创建的过程中,生成器不需要输入参数,直接是生成一些列Father的子类对象。
我们修改一下上面的代码,使它满足foreach的使用(就是实现Iterable接口):
package com.ray.ch13; import java.util.Iterator; import java.util.Random; public class Test implements Generator<Father>, Iterable<Father> { public Test() { } private int size = 0; public Test(int size) { this.size = size; } private Class<?>[] classes = { Sub1.class, Sub2.class, Sub3.class }; private Random random = new Random(); @Override public Father next() { Father father = null; try { father = (Father) classes[random.nextInt(3)].newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return father; } public static void main(String[] args) { Test test = new Test(); for (int i = 0; i < 5; i++) { System.out.println(test.next()); } System.out.println("------------------"); for (Father father : new Test(5)) { System.out.println(father); } } @Override public Iterator<Father> iterator() { return new FatherIterator(); } class FatherIterator implements Iterator<Father> { private int count = size; @Override public boolean hasNext() { return count > 0; } @Override public Father next() { count--; return Test.this.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } } } interface Generator<T> { T next(); } class Father { private static int counter = 0; private final int id = counter++; @Override public String toString() { return "name:" + getClass().getSimpleName() + " id:" + id; } } class Sub1 extends Father { } class Sub2 extends Father { } class Sub3 extends Father { }
输出:
name:Sub3 id:0
name:Sub1 id:1
name:Sub3 id:2
name:Sub3 id:3
name:Sub3 id:4
------------------
name:Sub3 id:5
name:Sub1 id:6
name:Sub1 id:7
name:Sub2 id:8
name:Sub3 id:9
参数化的接口确保next()的返回类型。
我们下面再看另一个例子,是著名的数学题目斐波那契数列:
package com.ray.ch13; public class Test implements Generator<Integer> { private Integer count = 0; @Override public Integer next() { return fib(count++); } private Integer fib(int param) { if (param < 2) { return 1; } return fib(param - 2) + fib(param - 1); } public static void main(String[] args) { Test test = new Test(); for (int i = 0; i < 10; i++) { System.out.print(test.next() + " "); } } } interface Generator<T> { T next(); }
输出:
1 1 2 3 5 8 13 21 34 55
总结:这一章节主要展示了泛型接口的使用。
这一章节就到这里,谢谢。
-----------------------------------
时间: 2024-11-29 07:06:00