Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package com.itheima.first;
  2. import java.util.*;
  3.  
  4. interface Animal
  5. {
  6. String getName();
  7. String cry();
  8. }
  9. class Dog implements Animal
  10. {
  11. private String name;
  12.  
  13. public Dog(String name)
  14. {
  15. this.name = name;
  16. }
  17.  
  18. public String getName()
  19. {
  20. return name;
  21. }
  22.  
  23. public String cry()
  24. {
  25. return "woof! woof!";
  26. }
  27. }
  28. class Cat implements Animal
  29. {
  30. private String name;
  31.  
  32. public Cat(String name)
  33. {
  34. this.name = name;
  35. }
  36.  
  37. public String getName()
  38. {
  39. return name;
  40. }
  41.  
  42. public String cry()
  43. {
  44. return "Meow!";
  45. }
  46. }
  47. public class Application {
  48. public static void main(String args[])
  49. {
  50.  
  51. ArrayList<Animal> animals = new ArrayList<Animal>();
  52. animals.add(new Dog("Bala"));
  53. animals.add(new Cat("Bolo"));
  54.  
  55. for (Animal animal: animals)
  56. {
  57. System.out.println(animal.getName());
  58. System.out.println(animal.cry());
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement