Generic 泛型 在method 中 传参的使用

 1 import java.util.*;
 2 
 3 
 4 /**
 5  * Created by wojia on 2017/6/14.
 6  */
 7 public class App {
 8     public static void main(String args[]){
 9         /**that means the tyoe of data being sended to generic must be String type*/
10         genericDemo<String> generic = new genericDemo<String>();
11         //clone
12         String temp = generic.send( "testing" );
13         System.out.println(temp);
14         //restricted in Integer type
15         ArrayList<Integer> list2 = new ArrayList<>();
16         list2.add( 1111 );
17         /** the type is determined as Integer!! */
18         doWork( list2 );
19     }
20 
21     /**
22      * <?> means the type is up to caller
23      *
24      * */
25     public static  ArrayList<?> doWork(ArrayList<?> list){
26         ArrayList<?> list1 = new ArrayList<>();
27         // acturally the below operation just pass the address(hashSet)
28         list1 = list;
29         System.out.println(list1);
30         return list1;
31     }
32 }

如上

posted on 2017-06-15 16:22  吸毒术士柯震东  阅读(283)  评论(0编辑  收藏  举报

导航