Exemple #1
0
        public static void Main()
        {
            IShape[] shapes = new IShape[6];

            shapes[0] = new Circle(1.12);
            shapes[1] = new Circle(3.44);
            shapes[2] = new Rectangle(34.4, 44);
            shapes[3] = new Rectangle(11.4, 23);
            shapes[4] = new Square(111.4, 12);
            shapes[5] = new Square(34.4, 44);

            foreach (var shape in shapes)
            {
                Type currentShapeType = shape.GetType();
                string shapeName = currentShapeType == typeof (Square) ?
                    "Square" : currentShapeType == typeof (Rectangle) ?
                    "Rectangle" : "Circle";

                Console.WriteLine("The area of the {0} is {1}. The perimeter is {2}",
                    shapeName, shape.CalculateArea(), shape.CalculatePerimeter());
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            ////Ack! Illegal to allocate interface types.
            //IPointy p = new IPointy();//Compiler erro!
            Console.WriteLine("****** Fun with Interfaces *****\n");
            //Call Points property defined by IPointy.
            Hexagon hex = new Hexagon();

            Console.WriteLine("Points: {0}", hex.Points);
            Console.WriteLine("*********");
            //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);
            }
            //Can we treat hex2 as Ipointy?
            Hexagon hex2   = new Hexagon("Peter");
            IPointy itfPt2 = hex2 as IPointy;

            if (itfPt2 != null)
            {
                Console.WriteLine("Points: {0}", itfPt2.Points);
            }
            else
            {
                Console.WriteLine("OOP! Not pointy...");
            }
            Console.WriteLine("****************************");
            //Makes 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);

            Console.ReadLine();
        }