fork download
  1. import java.util.*;
  2.  
  3. interface function<T> { public void func (T x); }
  4.  
  5. abstract class Foo { int n; abstract int foo(); }
  6. class Bar extends Foo { Bar (int x) { n = x; } int foo() { return n + 1; } }
  7. class Baz extends Foo { Baz (int x) { n = x; } int foo() { return n * 2; } }
  8.  
  9. class Main
  10. {
  11. public static void runClosure (function<Integer> f) {
  12. for (int i = 0; i < 100; i++) { f.func(i); }
  13. }
  14. public static void main (String args[]) {
  15. final Foo[] x = new Bar[]{ new Bar(0) };
  16. runClosure(new function<Integer>() {
  17. public void func(Integer n) {
  18. System.out.println(x[0].foo());
  19. x[0] = n % 2 == 0 ? new Bar(x[0].foo()) : new Baz(x[0].foo());
  20. }
  21. });
  22. }
  23. }
Runtime error #stdin #stdout 0.07s 215552KB
stdin
Standard input is empty
stdout
1
2