Cat, dog, and parrot objects created with specific attributes and behaviors
Primary constructor used to assign values to properties, set clause removed to maintain encapsulation
Code repetition identified as a maintenance headache; DRY principle will be introduced in next stage in order to avoid it!
OOP principles highlighted as important for efficient and maintainable applications.
Cat.cs
namespaceMyApplication.Animals;publicclassCat(string name){publicstring Name { get; } = name;publicvoidEat() {Console.WriteLine($"{Name} is eating..."); }publicvoidClimb() {Console.WriteLine($"{Name} is climbing on a roof!"); }}
Dog.cs
namespaceMyApplication.Animals;publicclassDog(string name){publicstring Name { get; } = name;publicvoidEat() {Console.WriteLine($"{Name} is eating..."); }publicvoidClimb() {Console.WriteLine($"{Name} is climbing on steps!"); }}
Parrot.cs
namespaceMyApplication.Animals;publicclassParrot(string name){publicstring Name { get; } = name;publicvoidEat() {Console.WriteLine($"{Name} is eating..."); }publicvoidClimb() {Console.WriteLine($"{Name} is climbing on a tree!"); }publicvoidFly() {Console.WriteLine($"{Name} is Flying!"); }}
Program.cs
usingMyApplication.Animals;var cat =newCat("Tom");Console.WriteLine($"{cat.Name} created!");cat.Eat();cat.Climb();Console.WriteLine();var dog =newDog("Mike");Console.WriteLine($"{dog.Name} created!");dog.Eat();dog.Climb();Console.WriteLine();var parrot =newParrot("Glory");Console.WriteLine($"{parrot.Name} created!");parrot.Eat();parrot.Climb();parrot.Fly();Console.WriteLine();