static void Main(string[] args) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); Animal jaba = new Frog("baba jaba", 1, Gender.Female); Animal kekerica = new Frog("kekerica", 3, Gender.Female); Animal sharo = new Dog("sharo", 3, Gender.Male); Animal sara = new Dog("sara", 2, Gender.Female); Animal oldy = new Dog("oldy", 12, Gender.Male); Animal puhi = new Kitten("puhi", 2); Animal tommy = new TomCat("tommy", 4); Animal mouseKiller = new Cat("mousy", 5, Gender.Male); List <Animal> animals = new List <Animal>() { jaba, kekerica, sharo, sara, puhi, tommy, mouseKiller, oldy }; var groupedAnimals = from animal in animals group animal by(animal is Cat) ? typeof(Cat) : animal.GetType() into gr select new { GroupName = gr.Key, AverageAge = gr.ToList().Average(an => an.Age) }; foreach (var animal in groupedAnimals) { Console.WriteLine("{0} - average age: {1:N2}", animal.GroupName.Name, animal.AverageAge); } }
static void Main(string[] args) { Console.WriteLine("\n封装、继承、多态"); Cat cat = new Cat("cat_name"); cat.ShoutNum = 6; cat.Shout(); Console.WriteLine("\n接口"); MachineCat machineCat = new MachineCat("Ding dang"); machineCat.ShoutNum = 5; machineCat.ChangeThing("Time."); Console.WriteLine("\n集合与泛型"); Register register = new Register(); register.register(); register.competition(); Console.WriteLine("\n委托与事件"); TomCat tomCat = new TomCat("Tom"); Mouse mouse1 = new Mouse("Jerry"); Mouse mouse2 = new Mouse("Jack"); // 将Mouse的Run方法通过实例化委托TomCat.TomCatShouEventHandler登记到tomCat的TomCatShout当中 tomCat.TomCatShout += new TomCat.TomCatShouEventHandler(mouse1.Run); tomCat.TomCatShout += new TomCat.TomCatShouEventHandler(mouse2.Run); tomCat.Shout(); }
static void Main(string[] args) { try { Frog Goshko = new Frog("Goshko", 10, "male"); Frog Dimitrur = new Frog("Dimitur", 6, "male"); Frog Minka = new Frog("Minka", 3, "female"); Dog Rex = new Dog("Rex", 1, "male"); Dog Hunter = new Dog("Hunter", 5, "male"); Dog Blondie = new Dog("Blondie", 3, "female"); Kitten Puxy = new Kitten("Puxy", 2); Kitten Dazzy = new Kitten("Dazzy", 5); Kitten Tuffy = new Kitten("Tuffy", 4); TomCat Tom = new TomCat("Tom", 4); TomCat Djeramaia = new TomCat("Djeramaia", 1); TomCat Virgin = new TomCat("Virgin", 7); IList <Animal> animals = new List <Animal> { Goshko, Dimitrur, Minka, Rex, Hunter, Blondie, Puxy, Dazzy, Tuffy, Tom, Djeramaia, Virgin }; Goshko.ProduceSound(); Rex.ProduceSound(); Puxy.ProduceSound(); Tom.ProduceSound(); double catsAverageAge = animals .Where(animal => animal is Cat) .Average(cat => cat.Age); double dogsAverageAge = animals .Where(animal => animal is Dog) .Average(dog => dog.Age); double frogsAverageAge = animals .Where(animal => animal is Frog) .Average(frog => frog.Age); Console.WriteLine("Frogs average age is: {0:F2}", frogsAverageAge); Console.WriteLine("Dogs average age is: {0:F2}", dogsAverageAge); Console.WriteLine("Cats average age is: {0:F2}", catsAverageAge); } catch (ArgumentOutOfRangeException ae) { Console.WriteLine(ae.Message); } catch (ArgumentNullException ae) { Console.WriteLine(ae.Message); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } }
static void Main() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Test The Animals..."); Console.ResetColor(); Console.WriteLine(); TomCat tomy = new TomCat("Tom", 3); Console.WriteLine("TomCat - " + tomy); tomy.ProduceSound(); Console.WriteLine(); Dog rex = new Dog("Rex", 5, Gender.Male); Console.WriteLine("Dog - " + rex); rex.ProduceSound(); Console.WriteLine(); Frog flip = new Frog("Flip", 1, Gender.Male); Console.WriteLine("Frog - " + flip); flip.ProduceSound(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Average Age..."); Console.ResetColor(); Console.WriteLine(); List<Cat> listOfCats = new List<Cat>(); listOfCats.Add(new Cat("Joney", 5, Gender.Male)); listOfCats.Add(new Cat("Mike", 1, Gender.Male)); listOfCats.Add(new Cat("Simi", 5, Gender.Female)); double catsAverageAge = listOfCats.Average(x => x.Age); Console.WriteLine("List Of Cats Average Age: {0}", catsAverageAge); List<Dog> listOfDogs = new List<Dog>(); listOfDogs.Add(new Dog("Sharko", 3, Gender.Female)); listOfDogs.Add(new Dog("Foxy", 1, Gender.Female)); listOfDogs.Add(new Dog("Tommy", 5, Gender.Male)); double dogsAverageAge = listOfDogs.Average(x => x.Age); Console.WriteLine("List Of Dogs Average Age: {0}", dogsAverageAge); Console.WriteLine(); }
private static Animal[] CreateArray(int size) { var output = new Animal[size]; for (int i = 0; i < size; i++) { var name = Generators.GenerateName(random); var age = random.Next(0, 21); var sex = random.Next(0, 2); var type = random.Next(0, 6); Animal animal; try { switch (type) { case 0: animal = new Cat(name, age, (SexType)sex); break; case 1: animal = new Kitten(name, age, (SexType)sex); break; case 2: animal = new TomCat(name, age, (SexType)sex); break; case 3: animal = new Dog(name, age, (SexType)sex); break; default: animal = new Frog(name, age, (SexType)sex); break; } } catch (ArgumentException) { continue; } output[i] = animal; } return(output); }