Advertisement
Guest User

D Polimorfismo

a guest
Dec 11th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.71 KB | None | 0 0
  1. import std.stdio;
  2.  
  3.  
  4. class Animal
  5. {
  6.     string nome;
  7.  
  8.     this(string nome)
  9.     {
  10.         this.nome = nome;
  11.     }
  12.  
  13.     void andar()
  14.     {
  15.         writeln(this.nome ~ " andou!");
  16.     }
  17. }
  18.  
  19.  
  20. class Cao : Animal
  21. {
  22.     this(string nome)
  23.     {
  24.         super(nome);
  25.     }
  26.  
  27.     void latir()
  28.     {
  29.         writeln(this.nome ~ " latiu");
  30.     }
  31. }
  32.  
  33.  
  34. class Gato : Animal
  35. {
  36.     this(string nome)
  37.     {
  38.         super(nome);
  39.     }
  40.  
  41.     void miar()
  42.     {
  43.         writeln(this.nome ~ " miou");
  44.     }
  45. }
  46.  
  47.  
  48. /* Sistema */
  49. void processar(ref Animal animal)
  50. {
  51.     animal.andar();
  52. }
  53.  
  54.  
  55. void main()
  56. {
  57.     auto cao = new Cao("Lilico");
  58.     auto gato = new Gato("Fred");
  59.     Animal[] animais = [cao, gato];
  60.  
  61.     foreach(animal; animais)
  62.     {
  63.         processar(animal);
  64.     }
  65.  
  66.     cao.latir();
  67.     gato.miar();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement