fork(1) 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. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. Player player = new Player();
  12. Random random = new Random();
  13. for (int i = 0; i < 10; i++) {
  14. GameObject go = random.nextBoolean() ? new Chest() : new NPC();
  15. go.accept(player);
  16. }
  17. }
  18. }
  19.  
  20. class Player {
  21. void handle(Chest c) {
  22. System.out.println("opening a chest");
  23. }
  24. public void handle(NPC npc) {
  25. System.out.println("meeting NPC");
  26. }
  27. }
  28. abstract class GameObject {
  29. abstract void accept(Player p);
  30. }
  31. class Chest extends GameObject {
  32. @Override void accept(Player p) {
  33. p.handle(this);
  34. }
  35. }
  36. class NPC extends GameObject {
  37. @Override void accept(Player p) {
  38. p.handle(this);
  39. }
  40. }
Success #stdin #stdout 0.06s 32484KB
stdin
Standard input is empty
stdout
meeting NPC
opening a chest
meeting NPC
opening a chest
meeting NPC
meeting NPC
meeting NPC
meeting NPC
opening a chest
meeting NPC