fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8.  
  9. abstract class Exp { int f() { return 1; } }
  10. class Add extends Exp {
  11. Exp a1, a2;
  12. Add(Exp a1, Exp a2) { this.a1 = a1; this.a2 = a2; }
  13. int f() { return a1.f() + a2.f(); }
  14. }
  15. class Mult extends Exp {
  16. Exp a1, a2;
  17. Mult(Exp a1, Exp a2) { this.a1 = a1; this.a2 = a2; }
  18. int f() { return a1.f() + a2.f(); }
  19. }
  20. class Const extends Exp {
  21. int v;
  22. Const(int v) { this.v = v; }
  23. }
  24. class Test {
  25. public static void main(String[] args) {
  26. Exp e = new Add(new Mult(new Const(2), new Const(3)), new Const(4));
  27. System.out.println(e.f());
  28. }
  29. }
Success #stdin #stdout 0.06s 32336KB
stdin
Standard input is empty
stdout
3