示例#1
0
 static void Main(string[] args)
 {
     Dog dog = new Dog("Rexy",25,"male");
     Console.WriteLine(dog);
     Console.WriteLine("Press any key to hear Rexy bark..");
     Console.ReadKey();
     dog.ProduceSound();
     Console.WriteLine(Environment.NewLine);
     Frog frog = new Frog("Anabel", 25, "female");
     Console.WriteLine(frog);
     Console.WriteLine("Press any key to hear Anabel creek..");
     Console.ReadKey();
     frog.ProduceSound();
     Console.WriteLine(Environment.NewLine);
     Tomcat tomcat = new Tomcat("Spot", 25);
     Console.WriteLine(tomcat);
     Console.WriteLine("Press any key to hear Spot meow..");
     Console.ReadKey();
     tomcat.ProduceSound();
     Console.WriteLine(Environment.NewLine);
     Kitten kitten = new Kitten("Cassandra", 15);
     Console.WriteLine(kitten);
     Console.WriteLine("Press any key to hear Cassandra bark..");
     Console.ReadKey();
     kitten.ProduceSound();
 }
示例#2
0
        static void Main(string[] args)
        {
            Dog dog = new Dog("Rexy", 25, "male");

            Console.WriteLine(dog);
            Console.WriteLine("Press any key to hear Rexy bark..");
            Console.ReadKey();
            dog.ProduceSound();
            Console.WriteLine(Environment.NewLine);
            Frog frog = new Frog("Anabel", 25, "female");

            Console.WriteLine(frog);
            Console.WriteLine("Press any key to hear Anabel creek..");
            Console.ReadKey();
            frog.ProduceSound();
            Console.WriteLine(Environment.NewLine);
            Tomcat tomcat = new Tomcat("Spot", 25);

            Console.WriteLine(tomcat);
            Console.WriteLine("Press any key to hear Spot meow..");
            Console.ReadKey();
            tomcat.ProduceSound();
            Console.WriteLine(Environment.NewLine);
            Kitten kitten = new Kitten("Cassandra", 15);

            Console.WriteLine(kitten);
            Console.WriteLine("Press any key to hear Cassandra bark..");
            Console.ReadKey();
            kitten.ProduceSound();
        }
示例#3
0
        static void Main()
        {
            Cat cat1 = new Cat("Tom", 2, "Male");
            cat1.ProduceSound();
            Cat cat2 = new Cat("Minka", 3, "Female");
            cat2.ProduceSound();
            Cat cat3 = new Cat("Gosho", 4, "Male");
            cat3.ProduceSound();

            Dog dog1 = new Dog("Lassie", 3, "Female");
            dog1.ProduceSound();
            Dog dog2 = new Dog("Haruki", 4, "Male");
            dog2.ProduceSound();
            Dog dog3 = new Dog("Murdjo", 5, "Male");
            dog3.ProduceSound();

            Frog frog1 = new Frog("Prince", 1, "Male");
            frog1.ProduceSound();
            Frog frog2 = new Frog("Igor", 2, "Male");
            frog2.ProduceSound();
            Frog frog3 = new Frog("Jivka", 1, "Female");
            frog3.ProduceSound();

            Kitten kitten1 = new Kitten("Bira", 1);
            kitten1.ProduceSound();
            Kitten kitten2 = new Kitten("Chertichka", 4);
            kitten2.ProduceSound();
            Kitten kitten3 = new Kitten("Pepa", 2);
            kitten3.ProduceSound();

            Tomcat tomcat1 = new Tomcat("Coolio", 4);
            tomcat1.ProduceSound();
            Tomcat tomcat2 = new Tomcat("Pantelei", 3);
            tomcat2.ProduceSound();
            Tomcat tomcat3 = new Tomcat("Kotyo", 6);
            tomcat3.ProduceSound();

            Console.WriteLine(new string('=', 30));

            Animal[] animals = { cat1, cat2, cat3, dog1, dog2, dog3, frog1, frog2, frog3, kitten1, kitten2, kitten3, tomcat1, tomcat2, tomcat3 };

            double averageAge = animals.Average(x => x.Age);
            Console.WriteLine("Average age of all animals is {0:0.##} years.", averageAge);

            double averageAgeCat = animals.Where(x => x is Cat).Average(x => x.Age);
            Console.WriteLine("Average age of the cats is {0:0.##} years.", averageAgeCat);

            double averageAgeDog = animals.Where(x => x is Dog).Average(x => x.Age);
            Console.WriteLine("Average age of the dogs is {0:0.##} years.", averageAgeDog);

            double averageAgeFrog = animals.Where(x => x is Frog).Average(x => x.Age);
            Console.WriteLine("Average age of the frogs is {0:0.##} years.", averageAgeFrog);

            double averageAgeKitten = animals.Where(x => x is Kitten).Average(x => x.Age);
            Console.WriteLine("Average age of the kitten is {0:0.##} years.", averageAgeKitten);

            double averageAgeTomcat = animals.Where(x => x is Tomcat).Average(x => x.Age);
            Console.WriteLine("Average age of the tomcats is {0:0.##} years.", averageAgeTomcat);
        }
示例#4
0
        static void Main()
        {
            Frog   f1 = new Frog("Billy", 0.2, "m");
            Dog    d1 = new Dog("Liza", 4, "f");
            Cat    c1 = new Cat("Kotaksi", 2, "m");
            Kitten k1 = new Kitten("Raya", 1);
            Tomcat t1 = new Tomcat("Rijo", 2);

            f1.ProduceSound();
            d1.ProduceSound();
            c1.ProduceSound();
            k1.ProduceSound();
            t1.ProduceSound();

            Console.WriteLine();

            Animal[] animals = new Animal[]
            {
                new Frog("Billy", 0.2, "m"),
                new Dog("Liza", 4, "f"),
                new Cat("Kotaksi", 2, "m"),
                new Kitten("Raya", 1),
                new Tomcat("Rijo", 2),
                new Frog("Pesho", 2.1, "f"),
                new Dog("Beti", 5.4, "f"),
                new Cat("Kotka", 2, "f"),
                new Kitten("Spaska", 4),
                new Tomcat("Gosho", 3),
                new Frog("Marmot", 4, "m"),
                new Tomcat("Bizen", 0.5),
                new Dog("India", 2.5, "f"),
            };

            animals.
            GroupBy(animal => animal.GetType().Name).
            Select(group => new
            {
                name         = group.Key,
                averageYears = group.Average(animal => animal.Age)
            }).
            ToList().
            ForEach(group => Console.WriteLine("Group: {0}, average age: {1:F2} years!", group.name, group.averageYears));
        }