泛型可以让我们及早的发现错误,主要是在编译器发现错误。泛型是一种编程风格。类型更加灵活。
泛型实例:
package com.bjpowernode.generic;
public class GenericTest2<ABC> {
private ABC obj;
public ABC getObj() {
return obj;
}
public void setObj(ABC obj) {
this.obj = obj;
}
public static void main(String[] args)
{
GenericTest2<String> g=new GenericTest2<String>();
g.setObj("hello");
String s=g.getObj();
System.out.println(s);
}
}

如果不小心把String类型赋值给Integer类型变量,则有出错提示。
先把笔记粘贴过来,我系统整理后在编辑。