Пример #1
0
        public static void SurfaceAnimals()
        {
            Console.WriteLine("Pterodactyl I am!");
            Pterodactyl pterodactyl = new Pterodactyl();

            Console.WriteLine($"Tail: {pterodactyl.Tail}.");
            Console.WriteLine($"Location: {pterodactyl.Location}.");
            Console.WriteLine($"Feed Cost:${pterodactyl.FeedCost}.");
            Console.WriteLine($"# of Eyes: {pterodactyl.Eyes}.");
            Console.WriteLine($"Are they Safe: {pterodactyl.Safe}.");
            Console.WriteLine($"Flyer: {pterodactyl.Flyer()}.");
            Console.WriteLine($"Eat: {pterodactyl.Eat()}.");
            Console.WriteLine($"Speak: {pterodactyl.Speak()}.");

            Console.WriteLine("");
            Console.WriteLine("Flying-Spider I am!");
            FlyingSpider flyingSpider = new FlyingSpider();

            Console.WriteLine($"Tail: {flyingSpider.Tail}.");
            Console.WriteLine($"Location: {flyingSpider.Location}.");
            Console.WriteLine($"Feed Cost:${flyingSpider.FeedCost}.");
            Console.WriteLine($"# of Eyes: {flyingSpider.Eyes}.");
            Console.WriteLine($"Are they Safe: {flyingSpider.Safe()}.");
            Console.WriteLine($"Flyer: {flyingSpider.Flyer()}.");
            Console.WriteLine($"Eat: {flyingSpider.Eat()}.");
            Console.WriteLine($"Speak: {flyingSpider.Speak()}.");

            //Two Interfaces here!
            Console.WriteLine("");
            Console.WriteLine("T-Rex I am!");
            TRex rex = new TRex();

            Console.WriteLine($"Tail: {rex.Tail}.");
            Console.WriteLine($"Location: {rex.Location}.");
            Console.WriteLine($"Feed Cost:${rex.FeedCost}.");
            Console.WriteLine($"# of Eyes: {rex.Eyes}.");
            Console.WriteLine($"Are they Safe: {rex.Safe}.");
            Console.WriteLine($"Walking: {rex.Walking()}.");
            Console.WriteLine($"Eat: {rex.Eat()}.");
            Console.WriteLine($"Speak: {rex.Speak()}.");
            Console.WriteLine($"Drinking: {rex.Drinking()}.");
            Console.WriteLine($"Interface => Fun Tricks: {rex.FunTricks}.");
            Console.WriteLine($"Interface => Loves Humans: {rex.lovesHumans}.");

            Console.WriteLine("");
            Console.WriteLine("Brontosaurus I am!");
            Brontosaurs brontosaurs = new Brontosaurs();

            Console.WriteLine($"Tail: {brontosaurs.Tail}.");
            Console.WriteLine($"Location: {brontosaurs.Location}.");
            Console.WriteLine($"Feed Cost:${brontosaurs.FeedCost}.");
            Console.WriteLine($"# of Eyes: {brontosaurs.Eyes}.");
            Console.WriteLine($"Are they Safe: {brontosaurs.Safe}.");
            Console.WriteLine($"Walking: {brontosaurs.Walking()}.");
            Console.WriteLine($"Eat: {brontosaurs.Eat()}.");
            Console.WriteLine($"Speak: {brontosaurs.Speak()}.");
            Console.WriteLine($"Drinking: {brontosaurs.Drinking()}.");
        }
Пример #2
0
        public static void RunWeek3Classwork()
        {
            //Week 3 in class
            //Deconstructor
            //ClassMaterial.Week1.Week3.Square MySquare0 = new ClassMaterial.Week1.Week3.Square(11,22);


            ClassMaterial.Week1.Week3.Square MySquare = new ClassMaterial.Week1.Week3.Square(3, 3);  //

            //var (length, height) = MySquare;    //

            int length = 5, height = 5;//////////, top = 100;


            MySquare.Deconstruct(out length, out height);                     //////////, out top);

            System.Console.WriteLine("MySquare.Length = " + MySquare.Length); //

            (length, height) = MySquare;

            System.Console.WriteLine("2nd MySquare.Length = " + MySquare.Length);

            //ClassMaterial.Week1.Week3.Square MySquare2 = new ClassMaterial.Week1.Week3.Square(,);

            //MySquare.Deconstruct(out length, out height);

            System.Console.WriteLine(length);  //


            //ClassMaterial.Week1.Week3.Square MySquare3 = new ClassMaterial.Week1.Week3.Square(3,3);


            ClassMaterial.Week1.Week3.Square yourSquare =
                new ClassMaterial.Week1.Week3.Square {
                Length = 5, Height = 5, Color = "Blue"
            };

            Dinosaur dino1 = new Dinosaur();

            dino1.Size = 10;

            TRex tRex = new TRex();

            tRex.Size = 20;

            Dinosaur dino2 = new TRex();  //upcasting

            //Now play with private, public, protected and sealed of the
            //  base and child class to see
            //  what you can access with the dot operator
            dino2.Eat();

            Dinosaur dino3 = new Pterodactyl(); //upcasting

            dino3.Eat();


            Dinosaur dino4 = new Dinosaur();

            dino4 = (TRex)tRex;
            dino4.Eat();
            dino4 = (Pterodactyl)dino3;
            dino4.Eat();

            System.Console.WriteLine("Now let's try an IS:");

            Dinosaur[] dinoArray = { dino1, tRex, dino2, dino3 };

            foreach (Dinosaur item in dinoArray)
            {
                if (item is TRex)
                {
                    item.Eat();
                }

                /*
                 * if( item is Pterodactyl)
                 * {
                 *  item.Sleep();
                 *
                 * }
                 *
                 */
            }

            System.Console.WriteLine(Utility.AddTwoNumbers(10101, 1010));
            System.Console.WriteLine(Utility.AddTwoNumbers(10101, 1010));

            var dinoList = new List <Dinosaur>(10000);

            dinoList.Add(dino1);
            dinoList.Add(tRex);
            dinoList.Add(dino2);
            dinoList.Add(dino3);

            foreach (var item in dinoList)
            {
                Console.WriteLine(item);
            }

            dinoList.ForEach(item => Console.Write(item + ","));

            System.Console.WriteLine("Week3Classwork");
        }
Пример #3
0
        private static void Week3Dinosaurs()
        {
            //Dinosaur examples (week3 topics continued)
            Dinosaur dino1 = new Dinosaur();

            dino1.Size = 10;
            dino1.Eat();  //Dinosaur Eat

            TRex tRex = new TRex();

            tRex.Size = 201;
            tRex.Eat();                         //TRex Eat

            Dinosaur dino2 = new TRex();        //Upcasting (implicit)

            dino2.Eat();                        //TRex Eat

            Dinosaur dino3 = new Pterodactyl(); //Upcasting (implicit)

            dino3.Eat();                        //Pterodactyl Eat

            Dinosaur dino4 = new Dinosaur();    //Downcasting (explicit)

            dino4 = (TRex)tRex;
            dino4.Eat();  //TRex Eat


            /*
             * Console.WriteLine("--------------------------------");
             * Console.WriteLine("..........'as' example..........");
             * Console.WriteLine("--------------------------------");
             * //gives -- Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
             * //basically, trying to test if dino10 is a Pterodactyl.  It isn't, so returns as null
             * Dinosaur dino6 = dino10 as Pterodactyl;
             * dino6.Sleep();
             */

            Console.WriteLine("--------------------------------");
            Console.WriteLine("...array and 'is' example.......");
            Console.WriteLine("--------------------------------");
            Dinosaur[] dinoArray = { dino1, tRex, dino2, dino3 };

            foreach (Dinosaur item in dinoArray)
            {
                if (item is TRex)
                {
                    item.Eat();
                }

                if (item is Pterodactyl)
                {
                    item.Sleep();
                }
            }



            /*
             * Console.WriteLine(tRex.Size);
             * Dinosaur.Raptor dino2 = new Dinosaur.Raptor();
             * dino2.Skin = true;
             * dino2.Eat();
             * //call default constructor with object initializers (optional parameters, default values)
             * Square yourSquare = new Square {Length = 5, Height =5, Color = "Blue"};
             * Console.WriteLine("yourSquare Color is " + yourSquare.Color);
             */


            //deconstructor pattern call

            /*
             *  https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct
             *  https://stackoverflow.com/questions/40906305/c-sharp-7-0-deconstructor
             */
            /*
             * Square mySquare = new Square(3, 3);
             * var (length, height) = mySquare;
             * //the following 2 lines are interchageable with the above "var" line, same result
             * //int l, w;
             * //mySquare.Deconstruct(out l, out w);
             *
             * Console.WriteLine(mySquare.Length);
             * Console.WriteLine(length);
             */
        }