Exemple #1
0
        public static Mammal CreateMammal(MammalSpecies Species)
        {
            Mammal animalObj = null;//Animaltype unknown at this time.

            //Lets determine users choice of animal.
            switch (Species)
            {
                case MammalSpecies.Cat:
                    animalObj = new Cat();
                    break;
                case MammalSpecies.Dog:
                    animalObj = new Dog();
                    break;
                case MammalSpecies.Gorilla:
                    animalObj = new Gorilla();
                    break;

                default:
                    Debug.Assert(false, "Not implemented");
                    break;
            }

            //Set animal category
            animalObj.Category = CategoryType.Mammal;

            return animalObj;//Return created instance of object.
        }
        public static Animal CreateAnimal(string animalCategory, string animalName,int shoutNumber)
        {
            Animal result=null;
            switch (animalCategory)
            {
                case "貓":
                    result = new Cat(animalName);
                    break;
                case "狗":
                    result = new Dog(animalName);
                    break;
                case "羊":
                    result = new Sheep(animalName);
                    break;
            }
            result.ShoutNum = shoutNumber;

            return result;
        }
        public static Mammal CreateMammal(MammalSpecies species)
        {
            Mammal animalObj = null;//Object type not known at this time
            try
            {
                //Object type determined by late binding
                switch (species)
                {

                    case MammalSpecies.Cat:

                        animalObj = new Cat();//late binding

                        break;

                    case MammalSpecies.Dog:

                        animalObj = new Dog();//late binding

                        break;

                    default:
                        Debug.Assert(false, "To be completed!");
                        break;
                }

            }
            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();
            }
            finally
            {
                //set category
                animalObj.Category = CategoryType.Mammal;
            }
            return animalObj;
        }
        /// <summary>
        /// Method creates animal
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public Animal CreateAnimal(DataRow row)
        {
            string creature = null;

            string objID = row["ID"].ToString();
            string objName = row["Name"].ToString();
            int objAge = int.Parse(row["Age"].ToString());
            GenderType objGender = (GenderType)row["Gender"];
            CategoryType objCategory = (CategoryType)row["Category"];
            string objInfo = row["ExtraInfo"].ToString();

            //type now known at this time
            Animal animalObject = null;

            string ID = animalObject.ID;
            string nickName = animalObject.NickName;
            int age = animalObject.Age;
            CategoryType category = animalObject.Category;
            ;
            GenderType gender = animalObject.Gender;
            int numberOfTeeth = animalObject.NumberOfTeeth;
            bool quaranteen = animalObject.Quarantine;
            int numberOfDays = animalObject.NumberOfDays;

            if (creature == "Dog")
            {
                animalObject = new Dog(ID, nickName, age, category,  gender, numberOfTeeth,  quaranteen,  numberOfDays);
            }
            else if (creature == "Cat")
            {
                animalObject = new Cat();
            }

            return animalObject;
        }