基于接口的匿名类定义......也许? [英] Anonymous class definition based on interface... maybe?
问题描述
我在 Spring in Action 一书中看到了这个Java片段,但我对语言结构并不熟悉。
I saw this Java snippet in the book Spring in Action, but I'm not familiar with the language construct.
new RowMapper() {
public Object mapRow() throws SQLException, DataAccessException {
Motorist motorist = new Motorist();
motorist.setId(rs.getInt(1));
motorist.setEmail(rs.getString(2));
motorist.setPassword(rs.getString(3));
motorist.setFirstName(rs.getString(4));
motorist.setLastName(rs.getString(5));
return motorist;
}
}
根据 Spring文档,RowMapper是一个界面。它看起来像一个基于RowMapper接口的匿名类定义。 new
关键字有点令人困惑,让我想知道这是否也会创建一个匿名类的实例。我猜是的,因为如果该类没有名称,你将如何在定义它的行之后创建一个实例?
According the Spring documentation, RowMapper is an interface. It looks to me like an anonymous class definition based on the RowMapper interface. The new
keyword is a little confusing, making me wonder if this also creates one instance of the anonymous class. I would guess yes, because if the class has no name, how will you ever create an instance after the line that defines it?
可以任何人都确认我的猜测:
Can anyone confirm my guesses that:
- 这是一个基于RowMapper接口的匿名类定义,
- 它创建该类的单个实例?
推荐答案
这是一个基于RowMapper接口的匿名类定义
这正是它的本质。
它会创建该类的单个实例吗?
是的。这是正确的。
这篇关于基于接口的匿名类定义......也许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!