java 内部类求解
谁能给我解释一下下面这个程序中红色部分内容,抽象类不是不能被实例化吗?是否会与匿名类有关呢?谢谢各位啦。abstract class Base {
public Base(int i) {
System.out.print("Base constructor, i = " + i);
}
public abstract void f();
}
public class AnonymousConstructor {
public static Base getBase(int i) {
return new Base(i) {
{ System.out.print("Inside instance initializer"); }
public void f() {
System.out.print("In anonymous f()");
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();
}
}
output:
Base constructor, i = 47Inside instance initializerIn anonymous f()