java—泛型
<一>认识泛型
不用泛型:
1 class diqiujw{ 2 public Object jing; 3 public Object wei; 4 public Object getJing() { 5 return jing; 6 } 7 public void setJing(Object jing) { 8 this.jing = jing; 9 } 10 public Object getWei() { 11 return wei; 12 } 13 public void setWei(Object wei) { 14 this.wei = wei; 15 } 16 17 } 18 public class Test2 19 { 20 public static void main(String[] args){ 21 diqiujw dq=new diqiujw(); 22 dq.setJing(111); 23 dq.setWei(120); 24 int j=(Integer) dq.getJing(); 25 int w=(Integer) dq.getWei(); 26 System.out.print("经度:"+j+"纬度:"+w); 27 } 28 }
使用泛型:
class diqiu <T>{//设置一个不确定的值,通常为<T> public T jing; public T wei; public T getJing() { return jing; } public void setJing(T jing) { this.jing = jing; } public T getWei() { return wei; } public void setWei(T wei) { this.wei = wei; } } public class Test1{ public static void main(String[] args){ diqiu<String> dq=new diqiu<String>();//设置数据类型,可以是<String> ||<int> ||<float> ... dq.setJing("111"); dq.setWei("332"); System.out.println("经度:"+dq.getJing()+"纬度:"+dq.getWei()); } }
<二>构造方法中使用泛型:
1 class ceshi<T>{ 2 public T value; 3 public ceshi(T value){ 4 this.value=value; 5 } 6 public T getValue() { 7 return value; 8 } 9 public void setValue(T value) { 10 this.value = value; 11 } 12 } 13 public class Generic02 { 14 public static void main(String[] args) { 15 ceshi<String> cs=new ceshi<String>("鹏鹏"); 16 System.out.print(cs.getValue()); 17 18 } 20 }
<三>指定多个泛型:
1 class Gen<T,K>{ 2 public T table; 3 public K key; 4 public T getTable() { 5 return table; 6 } 7 public void setTable(T table) { 8 this.table = table; 9 } 10 public K getKey() { 11 return key; 12 } 13 public void setKey(K key) { 14 this.key = key; 15 } 16 } 17 public class Generic03 { 18 public static void main(String[] args) { 19 Gen<String,Integer> g=new Gen<String,Integer>(); 20 g.setTable("hello"); 21 g.setKey(2015); 22 System.out.print(g.getTable()+" "+g.getKey()); 23 } 24 }
<四>泛型——通配符:
1 class Gen<T>{ 2 public T value; 3 4 public T getValue() { 5 return value; 6 } 7 8 public void setValue(T value) { 9 this.value = value; 10 } 11 public String toString(){//为了能够方便具体得到key的值重写toString方法 12 return this.getValue().toString(); 13 } 14 } 15 public class Generic03 { 16 public static void main(String[] args) { 17 Gen<Integer> g=new Gen<Integer>(); 18 g.setValue(100); 19 Generic03.tell(g); 20 } 21 public static void tell(Gen<?> n){//通配符:<?> 22 System.out.print(n); 23 } 24 }
<五>泛型方法:
1 class Gen{ 2 public <T>T say(T s){ 3 return s; 4 } 5 } 6 public class Generic03 { 7 public static void main(String[] args) { 8 Gen g=new Gen(); 9 String str=g.say("可以是字符串"); 10 System.out.println(str); 11 int in=g.say(111); 12 System.out.println(in); 13 } 14 }
<六>泛型数组:
1 class Gen{ 2 public <T>void say(T arr[]){ 3 for (int i = 0; i < arr.length; i++) { 4 System.out.print(arr[i]); 5 } 6 System.out.println(); 7 } 8 } 9 public class Generic03 { 10 public static void main(String[] args) { 11 Gen g=new Gen(); 12 String arr2[]={"我","在","洛阳"}; 13 Integer arr[]={2015,11,23}; 14 g.say(arr); 15 g.say(arr2); 16 } 17 }
posted on 2015-11-23 12:54 System.out.print() 阅读(198) 评论(0) 编辑 收藏 举报