感谢这位大神:
http://blog.csdn.net/waldmer/article/details/12773021
1、泛型接口
1.1泛型接口的基本概念
1.2泛型接口实现的两种方式
定义子类:在子类的定义上也声明泛型类型
- interface Info<T>{
- public T getVar() ;
- }
- class InfoImpl<T> implements Info<T>{
- private T var ;
- public InfoImpl(T var){
- this.setVar(var) ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public T getVar(){
- return this.var ;
- }
- };
- public class GenericsDemo24{
- public static void main(String arsg[]){
- Info<String> i = null;
- i = new InfoImpl<String>("李兴华") ;
- System.out.println("内容:" + i.getVar()) ;
- }
- };
如果现在实现接口的子类不想使用泛型声明,则在实现接口的时候直接指定好其具体的操作类型即可:
- interface Info<T>{
- public T getVar() ;
- }
- class InfoImpl implements Info<String>{
- private String var ;
- public InfoImpl(String var){
- this.setVar(var) ;
- }
- public void setVar(String var){
- this.var = var ;
- }
- public String getVar(){
- return this.var ;
- }
- };
- public class GenericsDemo25{
- public static void main(String arsg[]){
- Info i = null;
- i = new InfoImpl("李兴华") ;
- System.out.println("内容:" + i.getVar()) ;
- }
- };
2、泛型方法
2.1定义泛型方法
- class Demo{
- public <T> T fun(T t){
- return t ;
- }
- };
- public class GenericsDemo26{
- public static void main(String args[]){
- Demo d = new Demo() ;
- String str = d.fun("李兴华") ;
- int i = d.fun(30) ;
- System.out.println(str) ;
- System.out.println(i) ;
- }
- };
2.2通过泛型方法返回泛型类的实例
- class Info<T extends Number>{
- private T var ;
- public T getVar(){
- return this.var ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo27{
- public static void main(String args[]){
- Info<Integer> i = fun(30) ;
- System.out.println(i.getVar()) ;
- }
- public static <T extends Number> Info<T> fun(T param){
- Info<T> temp = new Info<T>() ;
- temp.setVar(param) ;
- return temp ;
- }
- };
2.3使用泛型统一传入参数的类型
- class Info<T>{
- private T var ;
- public T getVar(){
- return this.var ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo28{
- public static void main(String args[]){
- Info<String> i1 = new Info<String>() ;
- Info<String> i2 = new Info<String>() ;
- i1.setVar("HELLO") ;
- i2.setVar("李兴华") ;
- add(i1,i2) ;
- }
- public static <T> void add(Info<T> i1,Info<T> i2){
- System.out.println(i1.getVar() + " " + i2.getVar()) ;
- }
- };
如果add方法中两个泛型的类型不统一,则编译会出错。
- class Info<T>{
- private T var ;
- public T getVar(){
- return this.var ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public String toString(){
- return this.var.toString() ;
- }
- };
- public class GenericsDemo29{
- public static void main(String args[]){
- Info<Integer> i1 = new Info<Integer>() ;
- Info<String> i2 = new Info<String>() ;
- i1.setVar(30) ;
- i2.setVar("李兴华") ;
- add(i1,i2) ;
- }
- public static <T> void add(Info<T> i1,Info<T> i2){
- System.out.println(i1.getVar() + " " + i2.getVar()) ;
- }
- };

3、泛型数组
- public class GenericsDemo30{
- public static void main(String args[]){
- Integer i[] = fun1(1,2,3,4,5,6) ;
- fun2(i) ;
- }
- public static <T> T[] fun1(T...arg){
- return arg ;
- }
- public static <T> void fun2(T param[]){
- System.out.print("接收泛型数组:") ;
- for(T t:param){
- System.out.print(t + "、") ;
- }
- }
- };
4、泛型的嵌套设置
Demo类中的info属性是Info类的这种属性,Info类本身需要两个泛型。
- class Info<T,V>{
- private T var ;
- private V value ;
- public Info(T var,V value){
- this.setVar(var) ;
- this.setValue(value) ;
- }
- public void setVar(T var){
- this.var = var ;
- }
- public void setValue(V value){
- this.value = value ;
- }
- public T getVar(){
- return this.var ;
- }
- public V getValue(){
- return this.value ;
- }
- };
- class Demo<S>{
- private S info ;
- public Demo(S info){
- this.setInfo(info) ;
- }
- public void setInfo(S info){
- this.info = info ;
- }
- public S getInfo(){
- return this.info ;
- }
- };
- public class GenericsDemo31{
- public static void main(String args[]){
- Demo<Info<String,Integer>> d = null ;
- Info<String,Integer> i = null ;
- i = new Info<String,Integer>("李兴华",30) ;
- d = new Demo<Info<String,Integer>>(i) ;
- System.out.println("内容一:" + d.getInfo().getVar()) ;
- System.out.println("内容二:" + d.getInfo().getValue()) ;
- }
- };
java泛型的一些知识点:Java泛型--泛型应用--泛型接口、泛型方法、泛型数组、泛型嵌套
原文:http://www.cnblogs.com/dark-passion/p/7762502.html