1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) { Sub.main(args); } } class Sub { public static void main(String[] args) { List<Animal> animals = new ArrayList<>(); animals.add(new Cat(3)); animals.add(new Mike(5)); animals.add(new Dog(7)); var breeder = new Breeder(); breeder.breed(animals); } } class u { public static <T> void pln(T t) { System.out.println(t); } public static <T> void pln() { System.out.println(); } } abstract class Animal { protected int age; protected String voice; public Animal(int age) { this.age = age; } public String toString() { var str = this.voice + "(" + this.age + ")"; return str; } public void cry() { System.out.println(this.toString()); } } class Cat extends Animal { public Cat(int age) { super(age); this.voice = "ニャー"; } } class Mike extends Cat { public Mike(int age) { super(age); this.voice = "ミケニャー"; } } class Dog extends Animal { public Dog(int age) { super(age); this.voice = "ワン"; } } class Breeder { public void breed(List<Animal> animals) { animals.stream().forEach((var animal) -> { System.out.println(animal); }); } } |
1 2 3 |
ニャー(3) ミケニャー(5) ワン(7) |