From 07f6af80ed6e1d0864d683fa6a15a5b5376f5a84 Mon Sep 17 00:00:00 2001 From: DmitryShi20 <92202725+DmitryShi20@users.noreply.github.com> Date: Fri, 13 Jan 2023 20:30:03 +0300 Subject: [PATCH] Create Polimorfizm.cs --- Polimorfizm.cs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Polimorfizm.cs diff --git a/Polimorfizm.cs b/Polimorfizm.cs new file mode 100644 index 0000000..f3b1840 --- /dev/null +++ b/Polimorfizm.cs @@ -0,0 +1,63 @@ +using System; + +abstract class Animal +{ + protected int age; + protected string sound; + + public Animal(int age) + { + this.age = age; + } + + public void SetAge(int age) + { + this.age = age; + } + + public abstract void MakeSound(); + + public override string ToString() + { + return $"Age: {age}, Sound: {sound}"; + } +} + +class Cat : Animal +{ + public Cat(int age) : base(age) + { + sound = "Meow"; + } + + public override void MakeSound() + { + Console.WriteLine("Meow"); + } +} + +class Dog : Animal +{ + public Dog(int age) : base(age) + { + sound = "Woof"; + } + + public override void MakeSound() + { + Console.WriteLine("Woof"); + } +} + +class Test +{ + static void Main(string[] args) + { + Animal[] animals = new Animal[] { new Cat(3), new Dog(5) }; + foreach (Animal animal in animals) + { + animal.MakeSound(); + Console.WriteLine(animal.ToString()); + } + } +}