java 基于接口的匿名类定义......也许?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5184445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Anonymous class definition based on interface... maybe?
提问by Mike M. Lin
I saw this Java snippet in the book Spring in Action, but I'm not familiar with the language construct.
我在Spring in Action一书中看到了这个 Java 片段,但我不熟悉语言结构。
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;
}
}
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 afterthe line that defines it?
根据Spring 文档,RowMapper 是一个接口。在我看来,它就像一个基于 RowMapper 接口的匿名类定义。这个new
关键字有点令人困惑,让我怀疑这是否也创建了匿名类的一个实例。我猜是的,因为如果类没有名称,您将如何在定义它的行之后创建实例?
Can anyone confirm my guesses that:
谁能证实我的猜测:
- this is an anonymous class definition based on the RowMapper interface, and
- it creates a single instance of that class?
- 这是一个基于 RowMapper 接口的匿名类定义,以及
- 它创建了该类的单个实例?
采纳答案by aioobe
This is an anonymous class definition based on the RowMapper interface
这是一个基于 RowMapper 接口的匿名类定义
That's precisely what it is.
正是如此。
It creates a single instance of that class?
它创建了该类的单个实例?
Yep. That's correct.
是的。没错。
回答by adarshr
That code is implementing the interface in an anonymous way.
该代码以匿名方式实现接口。
The syntax would be similar to:
语法类似于:
Runnable runnable = new Runnable() {
public void run() {
}
};
Note the semicolon at the end of the declaration. Here the runnable object, though holds the reference to the Runnable interface actually contains the implemented object. That's runtime polymorphism for you!
注意声明末尾的分号。这里的 runnable 对象虽然持有对 Runnable 接口的引用,但实际上包含已实现的对象。这就是运行时多态性!
回答by Aasmund Eldhuset
Your guesses are entirely correct. An anonymous class definition may be based on either a non-final class or on an interface, and you must implement all abstract (or interface) methods. The only available syntax for declaring anonymous classes is new
, which also has the effect of instantiating exactly one instance of the anonymous class (in the course of the program, though, many instances of the same anonymous class could be created, if this code is executed several times).
你的猜测完全正确。匿名类定义可以基于非最终类或接口,并且您必须实现所有抽象(或接口)方法。声明匿名类的唯一可用语法是new
,它也具有仅实例化匿名类的一个实例的效果(但在程序过程中,如果执行此代码,可能会创建同一匿名类的许多实例几次)。
回答by Margus
Interface tells what methods the built class instance should have or if thy are label interfaces, then what kind of behavior to associate with it.
接口告诉构建的类实例应该有哪些方法,或者如果你是标签接口,那么什么样的行为与它相关联。
Anonymous classes are classes that basically while instantiating a class instance thy are also extending it with custom code. So if you are instantiating a interface, then you must write all the methods described with that interface, and as long as you do at least that much, then compiler will be happy. This is what is done here.
匿名类基本上是在实例化类实例的同时使用自定义代码对其进行扩展的类。因此,如果您正在实例化一个接口,那么您必须编写该接口所描述的所有方法,只要您至少做了那么多,编译器就会很高兴。这就是这里所做的。
IS this is an anonymous class definition based on the RowMapper interface?
这是基于 RowMapper 接口的匿名类定义吗?
Yes. As you can see mapRow()
function has been written. And if you debug the code you can see, that is not a class of an instance of interface, but class that extends interface. In case of abstract class or just class, it would be same - extended. So if class is finalyou cant write anonymous class for it.
是的。如您所见,mapRow()
函数已编写完毕。如果你调试代码你可以看到,那不是接口实例的类,而是扩展接口的类。在抽象类或只是类的情况下,它是相同的 - 扩展。所以如果 class 是final你不能为它编写匿名类。
Does it create a single instance of that class?
它是否创建了该类的单个实例?
Well, it extends it and makes an instance of it. It will be single instance and any sequent call to it would result in a different class. If you debug the code, then you can even see different class names dynamically associated with it.
好吧,它扩展了它并创建了它的一个实例。它将是单个实例,对它的任何后续调用都会导致不同的类。如果您调试代码,那么您甚至可以看到与其动态关联的不同类名。
回答by abishkar bhattarai
Declaring Anonymous class and in below example it creates two instances .
声明匿名类,在下面的例子中它创建了两个实例。
public class Multithread {
void test(){
new Runnable() {
@Override
public void run() {
System.out.println("1");
}
}.run();
new Runnable() {
@Override
public void run() {
System.out.println("11");
}
}.run();}
public static void main(String[] args) {
new Multithread().test();
}
}
回答by Martin Klinke
Solely from the code above and without knowing about RowMapper, all you can assume is that a new anonymous class based on RowMapper (which may be an interface or a class) is instantiated.
仅从上面的代码来看,在不了解 RowMapper 的情况下,您只能假设一个基于 RowMapper(可能是接口或类)的新匿名类被实例化。