コード例 #1
0
ファイル: Program.cs プロジェクト: mjquito/sandbox_csharp
        static void Main(string[] args)
        {
            Shape[] myShapes = { new Triangle("Martin"), new Hexagon("Girl") };
            foreach (Shape i in myShapes) {
               i.Draw();
            }
            Hexagon hex = new Hexagon();
            Console.WriteLine("Points: {0}", hex.Points);

            // Three ways to check wheter a type supports a specific interface

            // One way
            Circle c = new Circle("Lisa");
            IPointy itfPt = null;
            try {
                itfPt = (IPointy)c;
            } catch (InvalidCastException ex) {
                Console.WriteLine(ex.Message);
            }

            // Second way
            Hexagon hex2 = new Hexagon("Peter");
            IPointy itfPt2 = hex2 as IPointy;
            if(itfPt2 != null)
                Console.WriteLine("Points: {0}", itfPt2.Points);
            else
                Console.WriteLine("OOPS! Not pointy...");

            // Third way
            if (hex2 is IPointy)
                Console.WriteLine("Points: {0}", itfPt2.Points);
            else
                Console.WriteLine("OOPS! Not pointy...");

            // Interface as Parameters
            Shape[] myShapes1= { new Hexagon(), new Circle(), new Triangle(), new ThreeDCircle("Jojo") };

            for (int i = 0; i < myShapes1.Length; i++) {
                // Can I draw you in 3D?
                if (myShapes1[i] is IDraw3D<Shape>)
                    DrawIn3D((IDraw3D<Shape>)myShapes1[i]);
            }

            // -----------------------------------------------------------
            // IEnumerable and IEnumerator
            // -----------------------------------------------------------
            // Any type supporting a method named GetEnumerator() can be evaluated by the foreach construct

            BoxOfShapes myBox = new BoxOfShapes();
            Console.WriteLine("***********IEnumerable and IEnumerable and IEnumerator ************");
            foreach (Shape s in myBox) {
                s.Draw();
            }

            // Last line
            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args) {
            // IPointy p = new IPointy(); ERROR
            Hexagon hex = new Hexagon();
            Console.WriteLine("Points: {0}", hex.Points);


            Circle c = new Circle("Lisa");
            try {
                IPointy cp = (IPointy)c;
                Console.WriteLine("Points: {0}", cp.Points);
            } catch (InvalidCastException ex) {
                Console.WriteLine(ex);
            }

            Hexagon hex2 = new Hexagon("Peter");
            IPointy itHex2 = hex2 as IPointy;
            if (itHex2 != null) {
                Console.WriteLine("Points: {0}", itHex2.Points);
            } else {
                Console.WriteLine("Oops, not a IPointy");
            }
            Console.WriteLine();

            Shape[] shapes = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("Jojo") };
            foreach (Shape s in shapes) {
                //s.Draw();
                if (s is IDraw3D) {
                    DrawIn3D((IDraw3D)s);
                } else {
                    s.Draw();
                }
                if (s is IPointy) {
                    Console.WriteLine("Points: {0}", ((IPointy)s).Points);
                } else {
                    Console.WriteLine("{0} is not a IPointy", s.PetName);
                }
            }
            Console.WriteLine();

            IPointy firstPointyItem = FindFirstPointyShape(shapes);
            Console.WriteLine("Points: {0}", firstPointyItem.Points);

            Console.ReadLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Shevstudy/ShevStudy
        static void Main(string[] args)
        {
            Hexagon hex = new Hexagon();
            Console.WriteLine(hex.Points);

            Circle c = new Circle();
            IPointy itfPt = null;

            try
            {
                itfPt = (IPointy)c;
                Console.WriteLine(itfPt.Points);
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Hexagon hex2 = new Hexagon();

            IPointy itfPt2 = hex as IPointy;

            if (itfPt2 != null)
            {
                Console.WriteLine(itfPt2.Points);
            }
            else
            {
                Console.WriteLine("Oops, not Pointy...");
            }

            Shape[] myShapes = { new Hexagon(), new Circle(), new ThreeDCircle(), new Triangle("Joe"), new Circle("JoJo") };

            for (int i = 0; i < myShapes.Length; i++)
            {
                myShapes[i].Draw();

                if (myShapes[i] is IPointy)
                {
                    Console.WriteLine("-> Points: {0}", ((IPointy)myShapes[i]).Points);
                }
                else
                {
                    Console.WriteLine("->{0}\'s not pointy!", myShapes[i].PetName);
                }
            }

            Shape[] myShapes2 = { new Hexagon(), new Circle(), new Triangle(), new Circle("JoJo2") };

            for (int b = 0; b < myShapes2.Length; b++)
            {
                if (myShapes2[b] is IDraw3D)
                {
                    DrawIn3D((IDraw3D)myShapes2[b]);
                }
            }

            IPointy firtPointInShapes = FindFirstPointyShape(myShapes);
            Console.WriteLine("The item has {0} points", firtPointInShapes.Points);

            Console.ReadLine();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Interfaces *****\n");

            // Call Points property defined by IPointy.
            Hexagon hex = new Hexagon();
            Console.WriteLine("Points: {0}", hex.Points);

            // Catch a possible InvalidCastException.
            Circle c = new Circle("Lisa");
            IPointy itfPt = null;
            try
            {
                itfPt = (IPointy)c;
                Console.WriteLine(itfPt.Points);
            }
            catch (InvalidCastException e)
            {
                Console.WriteLine(e.Message);
            }

            // Catch using as
            Hexagon hex2 = new Hexagon("Peter");
            IPointy itfPt2 = hex2 as IPointy;

            if (itfPt != null)
            {
                Console.WriteLine("Points: {0}", itfPt2.Points);
            }
            else
            {
                Console.WriteLine("OOPS! Not pointy...");
            }

            // Catch using is

            // Make an array of Shapes.
            Shape[] myShapes = {new Hexagon(), new Circle(),
                               new Triangle("Joe"), new Circle("JoJo")};

            for (int i = 0; i < myShapes.Length; i++)
            {
                // Recall the Shape base class defines an abstract Draw()
                // member, so all shapes know how to draw themselves.
                myShapes[i].Draw();

                // Who's pointy?
                if(myShapes[i] is IPointy)
                    Console.WriteLine("-> Points: {0}", ((IPointy) myShapes[i]).Points);
                else
                    Console.WriteLine("-> {0}\'s not pointy!", myShapes[i].PetName);

                // Can I draw you in 3D?
                if (myShapes[i] is IDraw3D)
                {
                    DrawIn3D((IDraw3D)myShapes[i]);
                }
                Console.WriteLine();

            }

            // Get first pointy item.
            // To be safe, you'd want to check firstPointyItem for null before proceeding.
            IPointy firstPointyItem = FindFirstPointyShape(myShapes);
            Console.WriteLine("The item has {0} points", firstPointyItem.Points);
            Console.WriteLine();

            // This array can only contain types that
            // implement the IPointy interface.
            IPointy[] myPointyObjects = { new Hexagon(), new Knife(), new Triangle(), new Fork(), new PitchFork() };

            foreach (var i in myPointyObjects)
            {
                Console.WriteLine("Object has {0} points.", i.Points);
            }

            Console.ReadKey();
        }