【笔记】java 泛型
java泛型有许多应用,下面挑几个重点的来说。
普通型 → 泛型的最基本应用,很简单就不说了。
通配型
1 class Point<K,V> 2 { 3 private K key; 4 private V value; 5 6 public void setKey(K key){ 7 this.key = key; 8 } 9 10 public K getKey(){ 11 return this.key; 12 } 13 14 public void setValue(V value){ 15 this.value = value; 16 } 17 18 public V getValue(){ 19 return this.value; 20 } 21 22 public String toString(){ 23 return this.value.toString(); 24 } 25 } 26 27 public class Test{ 28 public static void main(String[] args){ 29 Point<Number,String> p = new Point<Number,String>(); 30 p.setValue("Nothing"); 31 fun(p); 32 } 33 34 public static void fun(Point<?,?> p){//重点是这里,意思是接受的对象类型不限定 35 System.out.println(p); 36 } 37 }
受限泛型
1 class Point<K> 2 { 3 private K key; 4 5 public void setKey(K key){ 6 this.key = key; 7 } 8 9 public K getKey(){ 10 return this.key; 11 } 12 13 public String toString(){ 14 return this.key.toString(); 15 } 16 } 17 18 public class Test{ 19 public static void main(String[] args){ 20 Point<Number> p = new Point<Number>(); 21 p.setKey(1.22D); 22 fun1(p); 23 fun2(p); 24 } 25 26 public static void fun1(Point<? extends Object> p){// extends 意思是接受的对象类型必须要是Object类或Object类的子类 27 System.out.println(p); 28 } 29 30 public static void fun2(Point<? super Double> p){// super 意思是接受的对象类型必须包含有Double的子类 31 System.out.println(p); 32 33 } 34 }
同类但泛型类型不同的对象之间不能相互赋值。
class Point<K> { private K key; public void setKey(K key){ this.key = key; } public K getKey(){ return this.key; } public String toString(){ return this.key.toString(); } } public class Test{ public static void main(String[] args){ Point<Number> p = new Point<Number>(); Point<Object> p2 = null; p = p2; // 这里会发生编译错误,泛型类型不等 } }
泛型接口
1 interface info<K>{//接口上定义泛型 2 public K getKey(); 3 } 4 5 class Point<K> implements info<K>//泛型接口的子类,该子类同样是泛型,子类的泛型类型与接口的泛型类型需保持一致 6 { 7 private K key; 8 9 public Point(K key){ 10 this.setKey(key); 11 } 12 13 private void setKey(K key){ 14 this.key = key; 15 } 16 17 public K getKey() { 18 return this.key; 19 } 20 21 public String toString(){ 22 return this.key.toString(); 23 } 24 } 25 26 public class Test{ 27 public static void main(String[] args){ 28 info<Number> i = null; 29 i = new Point<Number>(222.222); 30 System.out.println(i.getKey()); 31 } 32 }
以上是对类及接口进行泛型定义,下面是对类方法进行泛型定义
class Point { public <T> T getValue(T t){//注意方法中泛型的写法,与类或接口的泛型写法不大一样 return t; } } public class Test{ public static void main(String[] args){ Point p = new Point(); int t = p.getValue(111); System.out.println(t); } }
通过泛型方法返回泛型实例对象
class Info<T extends Number>{ private T var; public T getVar(){ return var; } public void setVar(T var){ this.var = var; } public String toString(){ return this.var.toString(); } } public class Test{ public static void main(String agrs[]){ Info<Integer> i = fun(30); System.out.println(i.toString()); } //返回的泛型对象实例必须与泛型对象保持一致 public static <T extends Number> Info<T> fun(T param){ Info<T> temp= new Info<T>(); temp.setVar(param); return temp; } }
泛型数组
public class Test{ public static void main(String agrs[]){ 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.println("接受泛型数组"); for(T t:param){ System.out.println(t); } } }
泛型嵌套
class Info<T>{ private T var; public Info(T var){ this.setVar(var); } private void setVar(T var){ this.var = var; } public T getVar(){ return this.var; } } class Info2<S>{ private S info; public Info2(S info){ this.setInfo(info); } private void setInfo(S info){ this.info = info; } public S getInfo(){ return this.info; } } public class Test{ public static void main(String agrs[]){ Info2<Info<String>> d = null; Info<String> i = null; i = new Info<String>("Test"); d = new Info2<Info<String>>(i); System.out.println("content1:"+i.getVar().toString()); System.out.println("content2:"+d.getInfo().getVar().toString()); } }