2011-12-01 115 views
3

我知道C/C++/C#/ ActionScript/PHP,並已在所有這些工作。按我的知識,我們爲這段Java代碼在做什麼?

Object obj = new Object(); 

的Java做同樣的大部分時間,但昨天與Netbeans和JTable中擺動控制工作時,IDE生成的驗證碼是哪一種看起來有點怪我通常初始化對象。它是如何初始化一個對象的任何解釋?新的DefaultTableModel(..)後的{..}部分是什麼?

_model = new DefaultTableModel(
    new Object [][] { 
    }, 
    new String [] { 
     "Id", "Project Title", "Start Date", "Deadline", "Description", "PercentDone" 
    }) { 
     Class[] types = new Class [] { 
      java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class 
     }; 
     boolean[] canEdit = new boolean [] { 
      false, false, false, false, false, false 
     }; 

     @Override 
     public Class getColumnClass(int columnIndex) { 
       return types [columnIndex]; 
     } 

     @Override 
     public boolean isCellEditable(int rowIndex, int columnIndex) { 
      return canEdit [columnIndex]; 
     } 
    }; 
+0

看起來像某種重寫,以使表模型有六個只讀的數據字符串列。 –

回答

6

它被稱爲匿名類,結合了類定義和實例化。鏈接:


// creates a new instance of the DefaultTableModel class and assigns it 
// to a previously declared variable named _model. 
// Note that contrary to C++, starting a variable name with an underscore is 
// legal but discouraged as a convention in Java 
// (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) 
_model = new DefaultTableModel(

    // this is first parameter to the DefaultTableModel constructor taking 
    // a two-dimensional array of Objects as the first parameter, and an 
    // array of Objects as the second. This creates an empty 2D array. 
    new Object [][] { 
    }, 

    // this is the second parameter to the DefaultTableModel constructor. 
    // It creates an array of Strings initialized with the provided values 
    new String [] { 
     "Id", "Project Title", "Start Date", "Deadline", "Description", "PercentDone" 
    }) 

     // start of the redifinition of the DefaultTableModel class 
     { 

      // creates a member variable named types at the default visibility. 
      // This member is an array of Class objects, initialized with the 
      // provided values 
      Class[] types = new Class [] 
       java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class 
      }; 

      // creates a member variable named canEdit at the default visibility. 
      // This member is an array of booleans, all initialized to false. 
      boolean[] canEdit = new boolean [] { 
       false, false, false, false, false, false 
      }; 

      // annotation used by IDEs and the compiler specifying that the 
      // method that follows overrides a method in the DefaultTableModel 
      // class. If the methods does not actually override such a method, 
      // an error will be generated (e.g. due to a spelling mistake in the 
      // method name or the wrong parameters being declared) 
      @Override 

      // override of the default getColumnClass method 
      public Class getColumnClass(int columnIndex) { 
       return types [columnIndex]; 
      } 

      // see explanation above 
      @Override 

      // override of the default isCellEditable method 
      public boolean isCellEditable(int rowIndex, int columnIndex) { 
       return canEdit [columnIndex]; 
      } 

    // end of the anonymous class 
    }; 
// end of the constructor call and assignment to _model statement. 
); 
+0

感謝JRL,至少現在我知道它叫什麼。 – codefreak

+0

以及Class [] types = new Class []部分呢?是一個類的數據類型還是它的關鍵字? – codefreak

+1

這是一個[datatype](http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html),關鍵字是小寫。 – JRL