OnJava8-Examples/innerclasses/AnonymousConstructor.java

31 lines
683 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: innerclasses/AnonymousConstructor.java
// Creating a constructor for an anonymous inner class.
import static net.mindview.util.Print.*;
abstract class Base {
public Base(int i) {
print("Base constructor, i = " + i);
}
public abstract void f();
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
public class AnonymousConstructor {
public static Base getBase(int i) {
return new Base(i) {
{ print("Inside instance initializer"); }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void f() {
print("In anonymous f()");
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();
}
} /* Output:
Base constructor, i = 47
Inside instance initializer
In anonymous f()
*///:~