示例#1
0
文件: Animals.cs 项目: Risorm/OOP
        static void Main()
        {
            Kitten molly = new Kitten("Molly", 13);

            Console.WriteLine(molly);
            molly.ProduceSound();
            Frog kiro = new Frog("Kiro", 1, Gender.Female);

            Console.WriteLine(kiro);
            kiro.ProduceSound();
            Dog sharo = new Dog("Sharo", 5, Gender.Male);

            Console.WriteLine(sharo);
            sharo.ProduceSound();
            Tomcat pesho = new Tomcat("Pesho", 3);

            Console.WriteLine(pesho);
            pesho.ProduceSound();
            Cat pena = new Cat("Pena", 2, Gender.Female);

            Console.WriteLine(pena);
            pena.ProduceSound();

            Kitten dolly = new Kitten("Dolly", 2);
            Frog   biro  = new Frog("Biro", 3, Gender.Male);
            Dog    paro  = new Dog("Paro", 10, Gender.Female);
            Tomcat mesho = new Tomcat("Mesho", 1);
            Cat    mena  = new Cat("Mena", 1, Gender.Male);

            Animal[] animals = new Animal[]
            {
                molly, kiro, sharo, pesho, pena,
                dolly, biro, paro, mesho, mena
            };

            var averageAge = from o in animals
                             group o by new { GroupName = o.GetType().Name } into gr
                select new
            {
                gr.Key.GroupName,
                AvarageAge = gr.Average(o => o.Age)
            };

            foreach (var animal in averageAge)
            {
                Console.WriteLine(String.Format("Type: {0}, Average age: {1:0.##}", animal.GroupName, animal.AvarageAge));
            }
        }
示例#2
0
        static void Main()
        {
            Tomcat cat  = new Tomcat("jonny", 4);
            Kitten cat2 = new Kitten("Holly", 2);
            Frog   frog = new Frog("Jwey", 6, "male");
            Dog    dog  = new Dog("Sharo", 3, "male");

            cat.ProduceSound();
            cat2.ProduceSound();
            frog.ProduceSound();
            dog.ProduceSound();

            Animal[] animals =
            {
                cat,
                cat2,
                frog,
                dog,
                new Dog("Polly",    12, "female"),
                new Frog("Kolio",    2, "male"),
                new Tomcat("Nuni",  3),
                new Kitten("Loly",  9),
                new Frog("Josh",     1, "male"),
                new Kitten("Jenny", 5),
                new Dog("Bond",      5, "male"),
                new Tomcat("Josh", 14)
            };

            var dogs = from animal in animals
                       where animal is Dog
                       select animal;

            var frogs = from animal in animals
                        where animal is Frog
                        select animal;

            var cats = from animal in animals
                       where animal is Cat
                       select animal;

            Console.WriteLine("Dogs avg age: " + ((double)dogs.Sum(a => a.Age)) / dogs.Count());

            Console.WriteLine("Frogs avg age: " + ((double)frogs.Sum(a => a.Age)) / frogs.Count());

            Console.WriteLine("Cats avg age: " + ((double)cats.Sum(a => a.Age)) / cats.Count());
        }
示例#3
0
        static void Main()
        {
            Tomcat cat = new Tomcat("jonny", 4);
            Kitten cat2 = new Kitten("Holly", 2);
            Frog frog = new Frog("Jwey", 6, "male");
            Dog dog = new Dog("Sharo", 3, "male");
            cat.ProduceSound();
            cat2.ProduceSound();
            frog.ProduceSound();
            dog.ProduceSound();

            Animal[] animals = {
                cat, 
                cat2, 
                frog, 
                dog,
                new Dog("Polly", 12, "female"),
                new Frog("Kolio", 2, "male"),
                new Tomcat("Nuni", 3),
                new Kitten("Loly", 9),
                new Frog("Josh", 1, "male"),
                new Kitten("Jenny", 5),
                new Dog("Bond", 5, "male"),
                new Tomcat("Josh", 14)
            };

            var dogs = from animal in animals
                where animal is Dog
                select animal;

            var frogs = from animal in animals
                where animal is Frog
                select animal;

            var cats = from animal in animals
                where animal is Cat
                select animal;

            Console.WriteLine("Dogs avg age: " + ((double)dogs.Sum(a => a.Age)) / dogs.Count());

            Console.WriteLine("Frogs avg age: " + ((double)frogs.Sum(a => a.Age)) / frogs.Count());

            Console.WriteLine("Cats avg age: " + ((double)cats.Sum(a => a.Age)) / cats.Count());
        }
        static void Main()
        {
            Dog rex  = new Dog("Rex", 3, "male");
            Dog max  = new Dog("Max", 1, "male");
            Dog sara = new Dog("Sara", 6, "female");

            Frog frog   = new Frog("Frog", 7, "male");
            Frog jaba   = new Frog("Jaba", 4, "female");
            Frog kermit = new Frog("Kermit", 10, "male");

            Cat kitty    = new Kitten("Kitty", 2);
            Cat snowball = new Tomcat("Snowball", 5);

            List <Animal> animals = new List <Animal>()
            {
                rex,
                max,
                sara,
                frog,
                jaba,
                kermit,
                kitty,
                snowball
            };

            var groupedAnimals =
                from animal in animals
                group animal by animal.GetType().Name into g
                select new { GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age) };

            foreach (var animal in groupedAnimals)
            {
                Console.WriteLine("{0}s - average age: {1:N2}", animal.GroupName, animal.AverageAge);
            }

            rex.ProduceSound();
            kermit.ProduceSound();
            snowball.ProduceSound();
        }
示例#5
0
        static void Main(string[] args)
        {
            Dog dog = new Dog("sharo", AnimalSex.male, 18);
            Console.WriteLine(dog.ToString());

            Frog frog = new Frog("Kermit", AnimalSex.male, 20);
            Console.WriteLine(frog.ToString());

            Cat regularCat = new Cat("CAT", AnimalSex.female, 4);
            Console.WriteLine(regularCat.ToString());

            Kitten kitten = new Kitten("kittie", AnimalSex.female, 1);
            //Kitten kitten = new Kitten("kittie", AnimalSex.male, 1); - this will explode
            Console.WriteLine(kitten.ToString());

            Tomcat tomcat = new Tomcat("Topcat", AnimalSex.male, 2);
            //Tomcat tomcat = new Tomcat("Topcat", AnimalSex.female, 2); - this too will explode
            Console.WriteLine(tomcat.ToString());

            //sounds
            Console.WriteLine();
            Console.WriteLine(dog.ProduceSound());
            Console.WriteLine(frog.ProduceSound());
            Console.WriteLine(regularCat.ProduceSound());
            Console.WriteLine(kitten.ProduceSound());
            Console.WriteLine(tomcat.ProduceSound());
            Console.WriteLine();

            List<Dog> dogs = new List<Dog>();
            dogs.Add(dog);
            dogs.Add(new Dog("me4o", AnimalSex.female, 69));
            dogs.Add(new Dog("FU", AnimalSex.female, 96));
            double averageAge = dogs.Average((x) => x.Age);
            Console.WriteLine("Dogs average age: {0}", averageAge);

            List<Frog> frogs = new List<Frog>();
            frogs.Add(frog);
            frogs.Add(new Frog("Big Toad", AnimalSex.male, 2));
            frogs.Add(new Frog("Lesser Toad", AnimalSex.female, 2));
            averageAge = frogs.Average((x) => x.Age);
            Console.WriteLine("Frogs average age: {0}", averageAge);

            List<Kitten> kittens = new List<Kitten>();
            kittens.Add(kitten);
            kittens.Add(new Kitten("little kittenn", AnimalSex.female, 2));
            kittens.Add(new Kitten("big kitten", AnimalSex.female, 2));
            averageAge = kittens.Average((x) => x.Age);
            Console.WriteLine("Kittens average age: {0}", averageAge);
        }