Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. abstract class Animal {
  2.  
  3. protected def weight
  4.  
  5. public Animal(int weight) {
  6. this.weight = weight
  7. }
  8.  
  9. abstract String roar()
  10.  
  11. }
  12.  
  13. class Dog extends Animal {
  14.  
  15. public Dog(int weight) {
  16. super(weight)
  17. }
  18.  
  19. @Override
  20. String roar() {
  21. return "HAO HAO HAO"
  22. }
  23.  
  24.  
  25. }
  26.  
  27. class Cat extends Animal {
  28. public Cat(int weight) {
  29. super(weight)
  30. }
  31.  
  32. @Override
  33. String roar() {
  34. return "Miao Miii Mi"
  35. }
  36.  
  37. }
  38.  
  39. /*
  40. Aqui são declaradas duas variáveis com o mesmo tipo no caso Animal
  41. que possuem o método roar como abstrato que para cada objeto instanciado
  42. se obtém uma resposta diferente.
  43. */
  44. static void main(String[] args) {
  45. Animal dog = new Dog(32)
  46. Animal cat = new Cat(16)
  47.  
  48. println("Dog with ${dog.weight} pounds make ${dog.roar()}")
  49. println("Cat with ${cat.weight} pounds make ${cat.roar()}")
  50.  
  51. }
Add Comment
Please, Sign In to add comment