Untitled
unknown
plain_text
5 months ago
933 B
2
Indexable
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public abstract class Animal { public string Name { get; set; } public virtual void Sound() { Console.WriteLine("The animal makes a sound."); } } public class Dog : Animal { public override void Sound() { Console.WriteLine("The dog barks."); } } public class Cat : Animal { public override void Sound() { Console.WriteLine("The cat meows."); } } class Program { static void Main(string[] args) { Animal[] animals = new Animal[2]; animals[0] = new Dog { Name = "Buddy" }; animals[1] = new Cat { Name = "Whiskers" }; foreach (Animal animal in animals) { Console.WriteLine($"Name: {animal.Name}"); animal.Sound(); } } }
Editor is loading...
Leave a Comment