Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- Animal[] animals = { new Dog(), new Cat(), new Lion(), new Cow() };
- Scanner scanner = new Scanner(System.in);
- System.out.println("動物名を入力してください(いぬ、ねこ、らいおん、うし)");
- System.out.print("> ");
- String name = scanner.next();
- for (Animal animal : animals) {
- System.out.println(animal.echo(name));
- }
- }
- }
- class Animal {
- public String echo(String name) {
- return getName().equals(name) ? getVoice() : "";
- }
- protected String getName() {
- return "";
- }
- protected String getVoice() {
- return "";
- }
- }
- class Dog extends Animal {
- protected String getName() {
- return "いぬ";
- }
- protected String getVoice() {
- return "わおーん";
- }
- }
- class Cat extends Animal {
- protected String getName() {
- return "ねこ";
- }
- protected String getVoice() {
- return "にゃおーん";
- }
- }
- class Lion extends Animal {
- protected String getName() {
- return "らいおん";
- }
- protected String getVoice() {
- return "がおーん";
- }
- }
- class Cow extends Animal {
- protected String getName() {
- return "うし";
- }
- protected String getVoice() {
- return "もおーん";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement