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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值