Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- interface IAnimal
- {
- string Breathe();
- string Eat();
- string Sleep();
- }
- interface IHavePet
- {
- IAnimal GetPet();
- }
- class Human : IAnimal, IHavePet
- {
- Dog dog = new Dog();
- public string Breathe() => "Human.Breathe";
- public string Eat() => "Human.Eat";
- public string Sleep() => "Human.Sleep";
- public IAnimal GetPet() => dog;
- }
- class Dog : IAnimal
- {
- public string Breathe() => "Dog.Breathe";
- public string Eat() => "Dog.Eat";
- public string Sleep() => "Dog.Sleep";
- }
- class Program
- {
- static string GiveFood(IAnimal animal)
- => animal switch
- {
- IHavePet human => human.GetPet().Eat(),
- _ => animal.Eat()
- };
- static void Main()
- {
- Console.WriteLine("Human: " + GiveFood(new Human()));
- Console.WriteLine("Dog: " + GiveFood(new Dog()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement