コード例 #1
0
        static void Main(string[] args)
        {
            Animal duck=new Duck();
            Duck duck2= new Duck();
            Console.WriteLine("duck 1 walking:");
            duck.Walk();

            Console.WriteLine("duck 2 walking:");
            duck2.Walk();

            Console.WriteLine("duck 2 eating:");
            duck2.Eat();
            duck2.Eat("pizza");

            //apeleaza metoda din clasa de baza
            Console.WriteLine("duck 3 eating:");
            Animal duck3 = duck2;
            duck3.Eat();

            Console.WriteLine("Cat walking:");
            var cat = new Cat
            {
                Age = 10
            };
            cat.Walk();
            Console.WriteLine("Cat's age:");
            Console.WriteLine(cat.Age);
            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Duck duck = new Duck();

            duck.Eat();
            duck.Fly();
            duck.LayAnEgg();
            duck.Swim();
            duck.Walk();
            Console.WriteLine("**Duck is checked\n");

            Swan swan = new Swan();

            swan.Eat();
            swan.Fly();
            swan.LayAnEgg();
            swan.Swim();
            swan.Walk();
            Console.WriteLine("**Swan is checked\n");

            Console.WriteLine("**Duck as a IBird");
            IBird bird = new Duck(20, 10);

            bird.Eat();
            bird.Fly();
            bird.LayAnEgg();
            bird.Walk();

            Console.WriteLine("\n**Swan as a IBird");
            bird = new Swan(20, 10);
            bird.Eat();
            bird.Fly();
            bird.LayAnEgg();
            bird.Walk();

            Console.WriteLine("\n**Duck as a IWaterFowl");
            IWaterFowl waterFowl = new Duck(20, 1);

            waterFowl.Eat();
            waterFowl.Fly();
            waterFowl.LayAnEgg();
            waterFowl.Walk();
            waterFowl.Swim();

            Console.WriteLine("\n**Swan as a IWaterFowl");
            waterFowl = new Swan(10, 1);
            waterFowl.Eat();
            waterFowl.Fly();
            waterFowl.LayAnEgg();
            waterFowl.Walk();
            waterFowl.Swim();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: bagamoon/OOP-Training-1
        static void Main(string[] args)
        {
            #region  LiskovSubstitution

            Duck duck = DuckFactory.GetDuck(DuckType.Muscovy);
            duck.Move();
            duck.Quark();
            while (duck.Weight < 10)
            {
                duck.Eat();
            }

            #endregion

            #region LeastKnowledge

            var    unfriendlyStaff = new UnfriendlyStaff();
            Doctor doctor          = unfriendlyStaff.GetAvailableDoctor(DateTime.Today);
            doctor.BookTimeByPhone(DateTime.Today);

            var friendlyStaff = new FriendlyStaff();
            friendlyStaff.BookDoctor(DateTime.Today);

            #endregion

            #region IoC

            var noIoC = new NoIoC();
            noIoC.Running();
            noIoC.RidingBike();
            noIoC.Swimming();

            var injectByConstructor = new InjectByConstructor(new Sprint(), new RideRoadBike(), new FreeStyleSwim());
            injectByConstructor.Running();
            injectByConstructor.RidingBike();

            var injectBySetter = new InjectBySetter();
            injectBySetter.Run      = new Sprint();
            injectBySetter.RideBike = new RideRoadBike();
            injectByConstructor.Running();
            injectByConstructor.RidingBike();

            #endregion


            Console.ReadKey();
        }
コード例 #4
0
        public static void NoClientMain()
        {
            #region Old Code...
            ////Animal Object Declaration
            ////old way of non-abstract class
            ////Animal myAnimal = new Animal();
            ////Mammal Object Declaration
            ////Mammal myMammal = new Mammal();
            ////Primate Object Declaration
            ////Primate myPrimate = new Primate();
            ////Human Object Declaration
            //Human myHuman = new Human();
            ////Bird Object Declaration
            ////Bird myBird = new Bird();
            ////Duck Object Declaration
            //Duck myDuck = new Duck();
            ////Eagle Object Declaration
            ////Eagle myEagle = new Eagle();
            ////Fish Object Declaration
            ////Fish myFish = new Fish();
            ////Trout Object Declaration
            //Trout myTrout = new Trout();
            ////Shark Object Declaration
            ////Shark myShark = new Shark();

            //////Animal Method calls
            //////myAnimal.Eat();
            //////myAnimal.Reproduce();
            //////myAnimal.Move();

            //////Mammal Method calls
            //////myMammal.Nurse();
            //////myMammal.GiveLiveBirth();

            //////Primate Method calls
            //////myPrimate.UseLargeBrain();
            //////myPrimate.SwingFromTrees();
            //////myPrimate.FierclyProtectTerritory();

            //////Human Method calls
            //myHuman.Work();
            //myHuman.Play();
            //myHuman.Eat();
            //myHuman.Eat("chicken");
            //myHuman.Eat("mashed potatoes and green beans", "utensils");


            //////Bird Method calls
            //////myBird.LayEggs();
            //////myBird.Fly();

            //////Duck Method calls
            //myDuck.Swim();
            //myDuck.GetBeakSlappedOff();
            //myDuck.Eat();
            //myDuck.Eat("grass");
            //myDuck.Eat("grass", "mouth");
            ////Eagle Method calls
            ////myEagle.StunVictim();
            ////myEagle.Soar();

            ////Fish Method calls
            ////myFish.LayEggs();
            ////myFish.Swim();
            ////myFish.GiveLiveBirth();

            ////Trout Method calls

            ////Shark Method calls
            //myShark.KeepMovingToBreathe();
            //myShark.AttackBoats();
            #endregion
            bool pleaseContinue = true;
            //field for the menu
            string reply = "";
            //a do while declaration
            do
            {
                //displaying the menu
                DisplayMenu();
                //getting input from the user
                reply = Input();
                //using a switch case for their answer
                switch (reply)
                {
                //case 1 | Human
                case "1":
                    //clearing the screen
                    ClearScreen();
                    //creating a new human
                    Human myHuman = new Human();
                    myHuman.Name = "Karna";
                    myHuman.Age  = 25;
                    myHuman.Work();
                    myHuman.Play();
                    myHuman.Eat();
                    myHuman.Eat("chicken");
                    myHuman.Eat("mashed potatoes and green beans", "utensils");
                    Utilities.LogIt("NoClient::The user instantiated a human.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    //making sure to break
                    break;

                //case 2 | Duck
                case "2":
                    //clearing the screen
                    ClearScreen();
                    //creating a new Duck
                    Duck myDuck = new Duck();
                    myDuck.Name = "Mallard";
                    myDuck.Age  = 2;
                    myDuck.Swim();
                    myDuck.GetBeakSlappedOff();
                    myDuck.Eat();
                    myDuck.Eat("grass");
                    myDuck.Eat("grass", "mouth");
                    Utilities.LogIt("NoClient::The user instantiated a duck.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    break;

                //case 3 | Trout
                case "3":
                    //clearing the screen
                    ClearScreen();
                    //creating a new Trout
                    Trout myTrout = new Trout();
                    myTrout.Name = "Rainbow";
                    myTrout.Age  = 1;
                    myTrout.AvoidAnglers();
                    myTrout.Eat();
                    myTrout.Eat("baby fish");
                    myTrout.Eat("baby fish", "mouth");
                    Utilities.LogIt("NoClient::The user instantiated a trout.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    break;

                //Case 4 | Exit
                case "4":
                    //clearing the screen
                    ClearScreen();
                    //creating a new Platypus
                    ComposedPlatypus myPlat = new ComposedPlatypus();
                    myPlat.DuckPART.Name = "Platy";
                    myPlat.TroutPART.Age = 1;
                    myPlat.LayEggs();
                    myPlat.SwimLikeDuck();
                    myPlat.SwimLikeFish();
                    myPlat.Nurse();
                    Utilities.LogIt("NoClient::The user instantiated a platypus.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    break;

                //Case 5 | Exit
                case "5":
                    //clearing the screen
                    ClearScreen();
                    //setting pleaseContinue to false
                    pleaseContinue = false;
                    //logging that the user exited the menu.
                    Utilities.LogIt("NoClient::The user exited the NoClient's menu.\n",
                                    Utilities.MessageSeverity.INFORMATIONAL, true);
                    //breaking from the case
                    break;

                //the default case
                default:
                    //clearing the screen
                    ClearScreen();
                    //try to throw a new exception
                    try
                    {
                        //throwing my custom exception
                        throw new CustomExceptions();
                    }
                    //catching the exception
                    catch (CustomExceptions)
                    {
                        //logging the exception
                        Utilities.LogIt("You entered an invalid option in the NoClient menu!\nTry again!\n",
                                        Utilities.MessageSeverity.ERROR, true);
                    }
                    //breaking from the case
                    break;
                }
                //while pleaseContinue is true
            } while (pleaseContinue);
        }