这段Java代码在做什么?

3

我知道C/C++/C#/ActionScript/PHP,并在它们中都有工作经验。就我所知,我们通常将对象初始化为

Object obj = new Object();

大多数情况下,Java都是这样做的,但昨天在使用Netbeans和JTable Swing控件时,IDE生成了这段代码,看起来对我来说有点奇怪。如何解释它是如何初始化一个对象的?new 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];
         }
     };

看起来像是一种覆盖方式,使表模型具有六个只读字符串列的数据。 - Will Bickford
1个回答

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.
);

谢谢JRL,至少现在我知道它叫什么了。 - codefreak
那么Class[] types = new Class []部分呢?Class是数据类型还是关键字? - codefreak
1
它是一个数据类型,关键词为小写。 - JRL
1
@codefreak:我添加了逐行详细解释。 - JRL
我希望我们可以多次评分。我会为你做10次 :) - codefreak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接