コード例 #1
0
ファイル: Program.cs プロジェクト: 3A9C/ITstep
        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();
        }
コード例 #2
0
ファイル: Animals.cs プロジェクト: Penkov/OOP
        static void Main(string[] args)
        {
            // create some animals from each class and print them
            Dog dog = new Dog("Sharo", 3, Genders.Male);
            Console.WriteLine(dog);
            dog.FetchStick();

            Console.WriteLine();

            Frog frog = new Frog("Kermit", 33, Genders.Male);
            Console.WriteLine(frog);
            frog.Jump();

            Console.WriteLine();

            Kitten kitty = new Kitten("Kitty", 1);
            Console.WriteLine(kitty);
            kitty.Cry();

            Console.WriteLine();

            Tomcat tomcat = new Tomcat("Tom", 12);
            Console.WriteLine(tomcat);
            tomcat.Piss();

            Console.WriteLine();

            // create an array of Animals
            Animal[] animals = new Animal[]
            {
                new Dog("Rex",3, Genders.Male),
                new Frog("Kekerica", 1, Genders.Female),
                new Kitten("Pisi", 1),
                new Tomcat("Tom",2),
                new Dog("Erik", 4, Genders.Male),
                new Frog("Penka", 1, Genders.Female),
                new Kitten("Jasmin", 2),
                new Tomcat("Kolio",6),
                new Dog("Bender",2, Genders.Male),
                new Frog("Ginka", 6, Genders.Female),
                new Kitten("Tedy", 1),
                new Tomcat("Muncho",4),
            };

            // calculate the aveage age of each animal and print them
            var averageAge =
                from an in animals
                group an by new { GroupName = an.GetType().Name } into gr
                select new
                {
                    GroupName = gr.Key.GroupName,
                    AvarageAge = gr.Average(an => an.Age)
                };

            foreach (var animal in averageAge)
            {
                Console.WriteLine(String.Format("Group: {0}, AvarageAge: {1:0.00}.", animal.GroupName, animal.AvarageAge));
            }
        }
コード例 #3
0
ファイル: Animal.cs プロジェクト: Producenta/TelerikAcademy
 public static double Average(Animal[] array)
 {
     double total = 0;
     for (int i = 0; i < array.Length; i++)
     {
         total = total + array[i].Age;
     }
     return total / array.Length;
 }
コード例 #4
0
        static void Main(string[] args)
        {
            #region 索引器使用方法一
            //Person.Person p = new Person.Person();

            ////使用索引器的方式给两个属性赋值
            //p[0] = "callofduty"; //账号赋值
            //p[1] = "123456";     //密码赋值
            ////获取属性的值
            //Console.WriteLine(p.Name);
            //Console.WriteLine(p.Password);
            ////通过索引器取值
            //Console.WriteLine(p[0]);
            //Console.WriteLine(p[1]);
            #endregion

            Animal.Animal an = new Animal.Animal();
            //使用第一个索引
            an["Fish"]  = 20;
            an["Tiger"] = 30;
            an["Mouse"] = 5;

            Console.WriteLine(an["Fish"]);
            Console.WriteLine(an["Tiger"]);
            Console.WriteLine(an["Tiger"]);

            Console.WriteLine("=========================");

            //第二个索引器
            an[1] = "dog";
            an[2] = "cat";
            an[3] = "bird";

            Console.WriteLine(an[1]);
            Console.WriteLine(an[2]);
            Console.WriteLine(an[3]);

            Console.ReadKey();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: thrusanova/OOPBasics
        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);
            }
        }
コード例 #6
0
ファイル: Animal.cs プロジェクト: AnastasiyaRemeslova/Animal
        public Cell FindCellForEat()
        {
            Random rand = new Random();

            Cell[] rangeCells = Enviroment.GetRangeCells(RadiusOfSight, PositionY, PositionX);
            Cell   cell = Enviroment.GetCellByCoords(PositionY, PositionX), nextCell;
            int    minX = PositionX, minY = PositionY, maxX = PositionX, maxY = PositionY;

            for (int i = 0; i < rangeCells.Length; i++)
            {
                if (rangeCells[i] != null && CheckCellForEat(rangeCells[i]))
                {
                    List <Animal> animals = rangeCells[i].Animals;
                    for (int j = 0; j < animals.Count; j++)
                    {
                        Animal animal = animals.ElementAt(j);
                        if (IsPredator)
                        {
                            if (CheckAnimalForEat(animal) && animal.AverageWeight <= AverageWeight && animal != this)
                            {
                                if (i < 9)
                                {
                                    cell = rangeCells[i];
                                    return(cell);
                                }
                                else
                                {
                                    int min = RadiusOfSight * 2;
                                    for (int k = 0; k < 9; k++)
                                    {
                                        if (rangeCells[k] != null)
                                        {
                                            nextCell = rangeCells[k];
                                            if (((Math.Abs(rangeCells[i].PositionX - nextCell.PositionX) + Math.Abs(rangeCells[i].PositionY - nextCell.PositionY)) < min) && nextCell is WaterCell)
                                            {
                                                min  = Math.Abs(rangeCells[i].PositionX - nextCell.PositionX) + Math.Abs(rangeCells[i].PositionY - nextCell.PositionY);
                                                minX = nextCell.PositionX;
                                                minY = nextCell.PositionY;
                                            }
                                        }
                                    }
                                }
                                cell = Enviroment.GetCellByCoords(minY, minX);
                            }
                            if (minX == PositionX && minY == PositionY)
                            {
                                int next = rand.Next(9);
                                if (rangeCells[next] != null)
                                {
                                    cell = rangeCells[next];
                                }
                            }
                        }
                        else
                        {
                            if (CheckAnimalForEat(animal) && animal.AverageWeight > AverageWeight && animal != this)
                            {
                                int max = 0;
                                for (int k = 0; k < 9; k++)
                                {
                                    if (rangeCells[k] != null)
                                    {
                                        nextCell = rangeCells[k];
                                        if (nextCell.Food > 0)
                                        {
                                            if ((Math.Abs(rangeCells[i].PositionX - nextCell.PositionX) + Math.Abs(rangeCells[i].PositionY - nextCell.PositionY)) > max)
                                            {
                                                max  = Math.Abs(rangeCells[i].PositionX - nextCell.PositionX) + Math.Abs(rangeCells[i].PositionY - nextCell.PositionY);
                                                maxX = nextCell.PositionX;
                                                maxY = nextCell.PositionY;
                                            }
                                        }
                                    }
                                }
                                cell = Enviroment.GetCellByCoords(maxY, maxX);
                            }
                            if (maxX == PositionX && maxY == PositionY)
                            {
                                int next = rand.Next(9);
                                if (rangeCells[next] != null)
                                {
                                    cell = rangeCells[next];
                                }
                            }
                        }
                    }
                }
            }
            return(cell);
        }
コード例 #7
0
ファイル: Animal.cs プロジェクト: AnastasiyaRemeslova/Animal
 protected abstract bool CheckAnimalForPropagate(Animal animal);
コード例 #8
0
ファイル: Animal.cs プロジェクト: AnastasiyaRemeslova/Animal
 protected abstract bool CheckAnimalForEat(Animal animal);