Chan Chen Coding...

          Question Mark ? in Java


          Parent Class
           public   class  Parent  {
            // your code 
           } 
           
          Child Class
           public   class  Child  extends  Parent {
           //  your code 
           } 


           import  java.util.List;
           import  com.test.Child;
           import  java.util.ArrayList;

           public   class  Test  {
               public   static   void  doTest(List < Parent >  list) {
              } 
               public   static   void  main(String[] args) {
                  List < Parent >  parentList  =   new  ArrayList < Parent > ();
                  List < Child >  childList  =   new  ArrayList < Child > ();
                   doTest(parentList);
                   // Compile Error 
                   doTest(childList);
              } 

          See addAll Method in ArrayList

           public   boolean  addAll(Collection <?   extends  E >  c)  {
              Object[] a  =  c.toArray();
              int  numNew  =  a.length;
              ensureCapacity(size  +  numNew);   //  Increments modCount 
              System.arraycopy(a,  0 , elementData, size, numNew);
              size  +=  numNew;
              return  numNew  !=   0 ;
           }


          Modify Our Code
           import  java.util.ArrayList;
           import  java.util.List;


           public   class  Test  {
                   // Change Our Method at Here
                public   static   void  doTest(List <?   extends  Parent >  list) {
                } 
                
                public   static   void  main(String[] args) {
                   List < Parent >  parentList  =   new  ArrayList < Parent > ();
                   List < Child >  childList  =   new  ArrayList < Child > ();
                   doTest(parentList);
                    // Compile Correct    
                   doTest(childList) 
               } 


          -----------------------------------------------------
          Silence, the way to avoid many problems;
          Smile, the way to solve many problems;

          posted on 2012-11-23 15:47 Chan Chen 閱讀(273) 評論(0)  編輯  收藏 所屬分類: Scala / Java

          主站蜘蛛池模板: 扎囊县| 越西县| 中超| 安康市| 黔江区| 炎陵县| 新竹县| 大丰市| 闽清县| 横峰县| 赤壁市| 清水县| 漾濞| 洛川县| 琼结县| 山阳县| 梧州市| 分宜县| 抚松县| 高阳县| 喀喇| 金川县| 广西| 屏东市| 靖远县| 雷波县| 高青县| 石柱| 太湖县| 永年县| 盐亭县| 万全县| 常山县| 岫岩| 抚远县| 金乡县| 西吉县| 陈巴尔虎旗| 晋城| 阿拉善右旗| 揭西县|