fork download
  1. abstract class Animal {
  2. public String name;
  3.  
  4. public Animal(String name) {
  5. this.name = name;
  6. }
  7.  
  8. public void play() {}
  9. }
  10.  
  11. class Dog extends Animal {
  12. public Dog(String name) {
  13. super(name);
  14. }
  15.  
  16. public void play() {
  17. System.out.println("わんわん!" + name + "だよ");
  18. }
  19. }
  20.  
  21. class Cat extends Animal {
  22. private int sleep;
  23.  
  24. public Cat(String name, int sleep) {
  25. super(name);
  26. this.sleep = sleep;
  27. }
  28.  
  29. public void play() {
  30. System.out.println("にゃあ!" + name + "だよ");
  31. if(sleep == 1) {
  32. System.out.println("...でも寝る");
  33. }
  34. }
  35. }
  36.  
  37. class Pet
  38. {
  39. public static void main(String[] args) {
  40. Animal[] pets = {
  41. new Dog("ぽち"),
  42. new Cat("たま", 1)
  43. };
  44.  
  45. for(Animal pet: pets) {
  46. pet.play();
  47. }
  48. }
  49. }
  50.  
Success #stdin #stdout 0.14s 55596KB
stdin
Standard input is empty
stdout
わんわん!ぽちだよ
にゃあ!たまだよ
...でも寝る