【问题标题】:What's this Java code doing? [closed]这个 Java 代码在做什么? [关闭]
【发布时间】:2011-12-01 13:31:35
【问题描述】:

我了解 C/C++/C#/ActionScript/PHP 并且在所有这些方面都工作过。根据我的知识,我们通常将对象初始化为

Object obj = new Object();

Java 大部分时间都是这样做的,但昨天在使用 Netbeans 和 JTable 摆动控件时,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];
         }
     };

【问题讨论】:

  • 看起来像是某种覆盖,使表模型有六个只读字符串列。

标签: java swing netbeans


【解决方案1】:

它被称为匿名类,结合了类定义和实例化。链接:


逐行解释:

// 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 至少现在我知道它叫什么了。
  • 那么 Class[] types = new Class [] 部分呢? Class 是数据类型还是关键字?
  • 是datatype,关键字是小写的。
  • @codefreak:我添加了逐行详细说明。
  • 我希望我们能多次评价。我会为你做 10 次 :)
猜你喜欢
  • 2013-01-24
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 2014-02-20
  • 2012-09-30
  • 2017-08-19
  • 1970-01-01
  • 2010-10-03
相关资源
最近更新 更多