Esempio n. 1
0
        public static void DemoExplicitInterface()
        {
            Rectangle <int> rectangle = new Rectangle <int>("Rectangle With Explicit Inteface Implementation", 10, 30);
            IDraw3D         draw      = rectangle as IDraw3D;

            if (draw != null)
            {
                draw.Draw();
            }
            IDrawHologram drawHolo = rectangle as IDrawHologram;

            if (drawHolo != null)
            {
                drawHolo.Draw();
            }
        }
Esempio n. 2
0
 // I'll Draw anything!
 public static void DrawThisShapeIn3D(IDraw3D itf3d)
 {
     itf3d.Draw();
 }
Esempio n. 3
0
        public static int Main(string[] args)
        {
            Line l = new Line();

            l.Draw();

            IDraw3D itfDraw3d = (IDraw3D)l;

            itfDraw3d.Draw();

            // First way to obtain an interface:
            Circle  c = new Circle("Mitch");
            IPointy itfPt;

            try
            {
                itfPt = (IPointy)c;
                Console.WriteLine("Got interface using casting...");
            }
            catch (InvalidCastException e)
            { Console.WriteLine("OOPS!  Not pointy..."); }


            // Second way to obtain an interface:
            Hexagon hex = new Hexagon("Fran");
            IPointy itfPt2;

            itfPt2 = hex as IPointy;
            if (itfPt2 != null)
            {
                Console.WriteLine("Got interface using as keyword");
            }
            else
            {
                Console.WriteLine("OOPS!  Not pointy...");
            }


            // Third way to grab an interface.
            Triangle t = new Triangle();

            if (t is IPointy)
            {
                Console.WriteLine("Got interface using is keyword");
            }
            else
            {
                Console.WriteLine("OOPS!  Not pointy...");
            }

            Console.WriteLine();

            // The C# base class pointer trick.
            Shape[] s = { new Hexagon(), new Circle(), new Triangle("Joe"), new Circle("JoJo") };
            for (int i = 0; i < s.Length; i++)
            {
                s[i].Draw();

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


                // Can I draw you in 3D?
                if (s[i] is IDraw3D)
                {
                    DrawThisShapeIn3D((IDraw3D)s[i]);
                }
                Console.WriteLine();
            }
            return(0);
        }
 // I'll draw anyone supporting IDraw3D!
 public static void DrawIn3D(IDraw3D itf3d)
 {
     Console.WriteLine("-> Drawing IDraw3D compatible type");
     itf3d.Draw();
 }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Shape[] s = { new Hexagon(), new Circle(), new Triangle("Trident"),
                          new Circle("Ring") };



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

            Hexagon testHexagon = new Hexagon();  // my changes create new obj for test with explicied implimentes Interface..
            IDraw3D Qdraw3D     = testHexagon;

            Qdraw3D.Draw();
            DrawIn3D(testHexagon);
            testHexagon.Draw();
            IDraw3D draw3D = testHexagon;

            draw3D.Draw();

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



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

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

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

                Console.WriteLine("----------------------------");
            }

            #region Interfaces as return values
            // Attempt to get IPointy.
            Circle c = new Circle();
            // IPointy itfPt = c as IPointy;
            IPointy itfPt = ExtractPointyness(c);
            if (itfPt != null)
            {
                Console.WriteLine("Object has {0} points.", itfPt.Points);
            }
            #endregion

            #region Print all the members in IPointy array
            Console.WriteLine("\n***** Printing out members in IPointy array *****");
            IPointy[] myPointyObjects = new IPointy[] { new Hexagon(), new Spear(),
                                                        new Triangle(), new Fork(), new PitchFork() };

            for (int i = 0; i < myPointyObjects.Length; i++)
            {
                Console.WriteLine("Object has {0} points.", myPointyObjects[i].Points);
            }
            #endregion

            Console.ReadLine();
        }