这种类型的结构被称为anonymous class。
例子:
interface Mapper {
public double f(double x, double y);
}
Mapper m = new Mapper() {
@Override // this annotation is not mandatory, but good practice!
public double f(double x, double y) {
return 10 * Math.sin(x/10) * Math.cos(y/20) * x;
}
}
这将是一样的:如果你想要做一些快速
public class MyMapper implements Mapper {
@Override
public double f(double x, double y) {
return 10 * Math.sin(x/10) * Math.cos(y/20) * x;
}
}
// and then constructing like this:
Mapper mapper = new MyMapper();
这个概念/结构是很方便的。
与Java 8这类事情已经变得更加简单与Lambda Expressions:
Mapper mapper =() -> return 10 * Math.sin(x/10) * Math.cos(y/20) * x;
一本好书,关于lambda表达式在Java中8:Lambda Reference by Angelika Langer & Klaus Kreft
http://docs.oracle.com /javase/tutorial/java/javaOO/anonymousclasses.html – khelwood 2014-10-28 16:30:56