Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using System;
  2.  
  3. interface IAnimal
  4. {
  5.     string Breathe();
  6.     string Eat();
  7.     string Sleep();
  8. }
  9.  
  10. interface IHavePet
  11. {
  12.     IAnimal GetPet();
  13. }
  14.  
  15. class Human : IAnimal, IHavePet
  16. {
  17.     Dog dog = new Dog();
  18.  
  19.     public string Breathe() => "Human.Breathe";
  20.     public string Eat() => "Human.Eat";
  21.     public string Sleep() => "Human.Sleep";
  22.  
  23.     public IAnimal GetPet() => dog;
  24. }
  25.  
  26. class Dog : IAnimal
  27. {
  28.     public string Breathe() => "Dog.Breathe";
  29.     public string Eat() => "Dog.Eat";
  30.     public string Sleep() => "Dog.Sleep";
  31. }
  32.  
  33. class Program
  34. {
  35.     static string GiveFood(IAnimal animal)
  36.         => animal switch
  37.         {
  38.             IHavePet human => human.GetPet().Eat(),
  39.             _ => animal.Eat()
  40.         };
  41.  
  42.     static void Main()
  43.     {
  44.         Console.WriteLine("Human: " + GiveFood(new Human()));
  45.         Console.WriteLine("Dog: " + GiveFood(new Dog()));
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement