JDK 1.5 新功能使用实例之Generics
Generics 是JDK 1.5 一个最重要的特性,主要用来处理Collection。
以下代码在JDK 1.5 调试通过。
代码实例1: Demo.java
<table borderColor=#ffcc66 width=”90%” align=center bgColor=#e6e4dd border=1>
<tr> <td>package maoxiang.examples.jdk15.generics;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author 毛翔
*
* 演示如何使用Generics 特性。代码来自于 Generics 教程:
* http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
*
* Generics类似于C++中的模板。
* 区别:
* 1.
* 2.
*/
public class Demo {
public static void main(String[] args) {}
/**
* 最简单的用法
*/
public void Test1() {
// 以前的用法
//List myIntList = new LinkedList(); // 1
//myIntList.add(new Integer(0)); // 2
//Integer x = (Integer) myIntList.iterator().next(); // 3 需要强制转换
// 1.5 的用法
List myIntList = new LinkedList(); // 1’
myIntList.add(new Integer(0)); //2’
Integer x = myIntList.iterator().next(); // 3’
}
/**
* 匿名字符的用法
*/
public void Test2() {
List list = new ArrayList();
//通过匿名字符打印一个集合
Wildcards(list);
Wildcards1();
/*
* 如果 Wildcards2 定义为Wildcards2(List shapes)
* 以下调用错误
*/
Wildcards2(list);
}
public void Wildcards(Collection< ? > c) {
// 以前的用法
//Iterator i = c.iterator();
//for (int k = 0; k < c.size(); k++) {
//
log(i.next());
//}
//1.5 的用法
//Collection<?> c 表示
for (Object e : c) {
log(e);
}
}
public void Wildcards1() {
//Collection<?> c = new ArrayList();
//c.add(new Object()); // compile time error
//以上为错误的用法,因为不能确定 c 的类型 ,不能使用add ,但get可以 。正确的用法如下:
ArrayList c = new ArrayList();
c.add(”test”);
List< ? > list = c;
log(c.get(0));
}
public void Wildcards2(List< ? extends Shape> shapes) { //List shapes 定义只能接受List shapes,也不能接受 List
for (Shape s : shapes) {
s.draw();
}
//以下写法错误,因为为参数申明为 extends Shpape,无法确定Rectangle 为Shape子类,属于不安全调用
//shapes.add(0, new Rectangle());
Map allDrivers = new HashMap();
Census.addRegistry(allDrivers);
//以下写法允许,因为drivers明确定义,
List drivers = new ArrayList();
Census.add(drivers);
}
/**
* Generic Methods 的用法
*
*/
public void Test3() {
//适用于各种类型的函数
Object[] oa = new Object[100];
Collectionco = new ArrayList();
fromArrayToCollection(oa, co);// T inferred to be Object
String[] sa = new String[100];
Collection cs = new ArrayList();
fromArrayToCollection(sa, cs);// T inferred to be String
fromArrayToCollection(sa, co);// T inferred to be Object
Integer[] ia = new Integer[100];
Float[] fa = new Float[100];
Number[] na = new Number[100];
Collection cn = new ArrayList();
fromArrayToCollection(ia, cn);// T inferred to be Number
fromArrayToCollection(fa, cn);// T inferred to be Number
fromArrayToCollection(na, cn);// T inferred to be Number
fromArrayToCollection(na, co);// T inferred to be Object
//test.fromArrayToCollection(na, cs);// 错误用法
}
public void fromArrayToCollection(T[] a, Collection c) {
for (T o : a) {
//如果参数定义为 Collection< ? > c 以下写法错误
c.add(o); // compile time error
}
}
/**
* generics 嵌套用法
* @param shapes
*/
public void drawAll(List< ? extends Shape> shapes) {
List> history = new ArrayList>();
history.add(shapes);
for (Shape s : shapes) {
s.draw();
}
}
/**
*
*
*/
public void Test4() {
List l1 = new ArrayList();
List l2 = new ArrayList();
System.out.print(l1.getClass() == l2.getClass());
//打印为 true,
}
/**
* 错误用法
*/
public void Test5() {
Collection cs = new ArrayList();
//以下为错误用法
//if (cs instanceof Collection) { } // illegal
//以下为警告用法
//Collection cstr = (Collection) cs; // unchecked
// warning
}
public void Test6() {
//错误用法
//List[] lsa = new List[10]; // not really allowed
List< ? >[] lsa = new List< ? >[10]; // ok, array of unbounded wildcard
// type
Object o = lsa;
Object[] oa = (Object[]) o;
List li = new ArrayList();
li.add(new Integer(3));
oa[1] = li; // correct
//String s = lsa[1].get(0); // run-time error - ClassCastException
//String s = lsa[1].get(0); // run time error, but we were warned
String s = (String) lsa[1].get(0); // run time error, but cast is
// explicit
}
public void Test7() {
Sinks = null;
Sink s1 = null;
Collection cs = null;
String str = writeAll(cs, s1);
//String str = writeAll(cs, s); // 无效调用
Object obj = writeAll1(cs, s); // 正确的调用
str = writeAll2(cs, s1); // 正确的调用
}
public T writeAll(Collection coll, Sink snk) {
T last = null;
for (T t : coll) {
last = t;
snk.flush(last);
}
return last;
}
public T writeAll1(Collection< ? extends T> coll, Sink snk) {
T last = null;
for (T t : coll) {
last = t;
snk.flush(last);
}
return last;
}
public T writeAll2(Collection coll, Sink< ? super T> snk) {
T last = null;
for (T t : coll) {
last = t;
snk.flush(last);
}
return last;
}
// 打印
private void log(Object ob) {
System.out.print(ob);
}
}
//辅助定义
abstract class Shape {
public abstract void draw();
}
class C ircle extends Shape {
private int x, y, radius;
public void draw() {
}
}
class Rectangle extends Shape {
private int x, y, width, height;
public void draw() {
}
}
class Person {}
class Driver extends Person {}
class Census {
public static void addRegistry(Map registry) {
}
public static void add(List< ? extends Person> persons) {
}
}
class Collections {
public static void copy(List dest, List src) {
}
}
</td></tr></table>
代码实例2。Sink.java
<table borderColor=#ffcc66 width=”90%” align=center bgColor=#e6e4dd border=1>
<tr> <td>package maoxiang.examples.jdk15.generics;
/**
*
* @author 毛翔
*
* 定义一个接口模板,简化了接口的定义
*
*/
interface Sink {
public void flush(E t);
}
/*
* 如果是以前的定义,则要定义要各种类型的接口,显然更麻烦
* interface Sink {
*
* public void flush(String str);
* public void flush(Object obj);
* public void flush(Integer test);
* ……
* }
*/</td></tr></table>
|
本文所属类别其他文章
Java: | 技术IT: |













































