static void Main(string[] args) { Animal[] animals = new Animal[5]; animals[0] = new Cat("KUZYA"); animals[1] = new Bird("GOSHA"); animals[2] = new Bird("CARCUSHA"); animals[3] = new Cat("NUSHA"); animals[4] = new Bird("VASILIY"); foreach (var an in animals) { try { IFlier f = (IFlier)an; f.Fly(); } catch (InvalidCastException) { // nothing } } Console.ReadKey(); }
static void Main() { var dog = new Dog(); //Console.WriteLine(dog); //Console.WriteLine(dog.Greet()); var cat = new Cat(); //Console.WriteLine(cat.Greet()); var snake = new Snake(); //Console.WriteLine(snake.Greet()); var animals = new List<Animal>(); animals.Add(dog); animals.Add(cat); animals.Add(snake); animals.Add(new Dog()); animals.Add(new Dog()); var collectionOfDogs = animals.OfType<Dog>().Cast<Dog>(); foreach (var item in collectionOfDogs) { //item.Run(); } foreach (var item in animals) { //Console.WriteLine(item); int legs = item.GetNumberOfLegs(); Console.WriteLine(item.GetType().Name + " " + legs); } try { cat.Name = "buksi"; } catch (InvalidAnimalNameException ex) { Console.WriteLine(ex.Message); } }
static void Main(string[] args) { string animal = Console.ReadLine(); try { while (animal != "Beast!") { string line = Console.ReadLine(); string[] input = line.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries); string name = ""; int age = 0; string gender = ""; Animal newAnimal = null; if (input.Length == 3) { name = input[0]; age = int.Parse(input[1]); gender = input[2]; } else if (input.Length == 2) { name = input[0]; age = int.Parse(input[1]); if (name=="kitten" || name=="Kitten") { gender = "Female"; } else { gender = "Male"; } } else { throw new ArgumentException("Invalid input!"); } switch (animal.ToLower()) { case "animal": newAnimal = new Animal(name, age, gender); break; case "cat": newAnimal = new Cat(name, age, gender); break; case "dog": newAnimal = new Dog(name, age, gender); break; case "frog": newAnimal = new Frog(name, age, gender); break; case "kitten": newAnimal = new Kitten(name, age, gender); break; case "tomcat": newAnimal = new Tomcat(name, age, gender); break; } animal = Console.ReadLine(); } } catch(Exception ex) { Console.WriteLine(ex.Message); } }
private void speakClick(object sender, RoutedEventArgs e) { if (times == 0) { p = new Pig(textBlock); d = new Dog(ref textBlock); c = new Cat(textBlock); Say += p.saying; Say += d.saying; Say += c.saying; } Random ran = new Random(); Say(this, new myEventArgs(ran.Next(1, 4))); times++; }
private void sureClick(object sender, RoutedEventArgs e) { if (times == 0) { Say += p.saying; Say += d.saying; Say += c.saying; } if (textBox.Text == "Pig" || textBox.Text == "pig") { Animal a = new Pig(textBlock); a.saying(sender, new myEventArgs(1)); } if (textBox.Text == "Dog" || textBox.Text == "dog") { Animal a = new Dog(ref textBlock); a.saying(sender, new myEventArgs(2)); } if (textBox.Text == "Cat" || textBox.Text == "cat") { Animal a = new Cat(textBlock); a.saying(sender, new myEventArgs(3)); } textBox.Text = ""; times++; }
public static void Main() { Cat cat = new Cat(); cat.Sleep(true); }