private void Speak_Click(object sender, RoutedEventArgs e) { Random rnd = new Random(); int random = rnd.Next(1, 4); switch (random) { case 1: Pig pig = new Pig(); string t1 = pig.Speak(sender, e); this.Output.Text += t1 + "\n"; break; case 2: Cat cat = new Cat(); string t2 = cat.Speak(sender, e); this.Output.Text += t2 + "\n"; break; case 3: Dog dog = new Dog(); string t3 = dog.Speak(sender, e); this.Output.Text += t3 + "\n"; break; } }
static void Main(string[] args) { Animal animal = new Animal(); animal.Name = "Animal"; animal.Speak(); Dog dog = new Dog(); dog.Name = "Spike"; animal = dog; dog.Speak(); Cat cat = new Cat(); cat.Name = "Tom"; animal = cat; cat.Speak(); Console.ReadKey(); }