Java-编译后出现$1.class与$2.class的情况总结

 这是因为在我们写的类中存在匿名类
1:
对于接口和抽象类来说, 不能被实例化.
看下面的代码
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
//do action
}
};
listener 是那个类的对象呢? 接口是不能实例化的.
我们说不出这个类的名字, 就叫它匿名类.
当一个类文件编译之后有很多类名字中有$符, 比如TestFrame.class, TestFrame$1.class, TestFrame$2.class, TestFrame$MyJob.class
$后面跟数字的类就是匿名类编译出来的结果.TestFrame$MyJob.class则是内部类MyJob编译后得到的.
使用内部类可以隐藏一些实现的细节, 等等, 还有其他一些好处. 使用匿名类的时候, 要注意代码的可读性.
为了更好的理解,下面列出了两个例子:
(1):   在类AbstractMMItemJdbcDaoImpl中的findProjectResult方法只要出现类似于new RowMapperResultReader(new RowMapper()情况,我们在编译类文件AbstractMMItemJdbcDaoImpl的时候,就会出现$1.class,$2.class的情况
return getJdbcTemplate().query(sql, new RowMapperResultReader(new RowMapper() {
   public Object mapRow(ResultSet rs, int _int) throws SQLException {

    ResultListPOJO resultListpojo = new ResultListPOJO();
    resultListpojo.setBillCode(rs.getString("billCode"));
    resultListpojo.setApplyBillCode(rs.getString("applyBillCode"));
    resultListpojo.setApplyDate(rs.getString("applyDate"));
    resultListpojo.setMmItemId(rs.getLong("mmItemId"));
    resultListpojo.setMmCode(rs.getString("mmCode"));
    resultListpojo.setMmName(rs.getString("mmName"));
    
    resultListpojo.setProjectCode(rs.getString("itemCode"));
    resultListpojo.setProjectName(rs.getString("itemName"));
    
    resultListpojo.setMmFullName(rs.getString("mmName")+ (rs.getString("specification") != null ? rs.getString("specification") : ""));    
    resultListpojo.setBillId(rs.getLong("billId"));
    resultListpojo.setBillType(rs.getLong("billType"));
    resultListpojo.setBillTypeName(rs.getString("billTypeName"));
    resultListpojo.setEngineerName(rs.getString("engineerName"));
    resultListpojo.setPrice(rs.getDouble("price"));
    resultListpojo.setUnitName(rs.getString("unitName"));
    resultListpojo.setStoreName(rs.getString("storeName"));
    resultListpojo.setWoFlagName(rs.getString("woFlagName"));
    
    resultListpojo.setPlanAmount(rs.getDouble("planAmount"));
    resultListpojo.setStoreAmount(rs.getDouble("storeAmount"));
    resultListpojo.setPlanSumMoney(rs.getDouble("price"), rs.getDouble("planAmount"));
    resultListpojo.setStoreSumMoney(rs.getDouble("price"), rs.getDouble("storeAmount"));
    
    resultListpojo.setApplyManStandardWeight(rs.getDouble("applyManStandardWeight"));
    resultListpojo.setDispatcherStandardWeight(rs.getDouble("dispatcherStandardWeight"));
    resultListpojo.setDispatcherActualWeight(rs.getDouble("dispatcherActualWeight"));
    resultListpojo.setStoremanActualWeight(rs.getDouble("storemanActualWeight"));
    
    resultListpojo.setInOrOut(rs.getInt("inOrOut"));
    return resultListpojo;
   }
  }));
(2):   Typical Use
Most commonly anonymous classes are used to field events in Listeners.


// This code goes in your Dialog class
// to shutdown the Dialog
// when the user clicks the Dismiss Button.
dismissButton.addActionListener
( new ActionListener()
     {
     /**
     * close down the Dialog when user clicks Dismiss
     */
     public void actionPerformed ( ActionEvent event )
        {
        Object object = event.getSource();
        if ( object == dismissButton )
           {
           dismiss();
           } // end if
        } // end actionPerformed
     } // end anonymous class
);   // end addActionListener line

你可能感兴趣的:(java)