예제 #1
0
 //const double PI = 3.14159;
 static void Main(string[] args)
 {
     try
     {
         Shape obj = ShapeFactory.createShape("triangle");
         //obj = ShapeFactory.createShape("circle");
         //obj = ShapeFactory.createShape("rectangle");
         //obj = ShapeFactory.createShape("square");
         obj.create();
         Console.WriteLine("The area is " + obj.Area());
     }
     catch (System.IndexOutOfRangeException e)
     {
         Console.WriteLine(e.Message);
     }
 }
예제 #2
0
        static void Main(string[] args)
        {
            int   flag;
            Shape myShape;

            Console.WriteLine("---------------------------");
            Console.WriteLine("1.Triangle      2.Circle");
            Console.WriteLine("3.Square        4.Rectangle");
            Console.WriteLine("---------------------------");
            Console.Write("请选择一种图形:");
            flag = Int32.Parse(Console.ReadLine());
            while (flag < 1 || flag > 4)
            {
                Console.Write("请重新输入:");
                flag = Int32.Parse(Console.ReadLine());
            }

            switch (flag)
            {
            case 1:
                myShape = ShapeFactory.createShape("Triangle");
                myShape.showArea();
                break;

            case 2:
                myShape = ShapeFactory.createShape("Circle");
                myShape.showArea();
                break;

            case 3:
                myShape = ShapeFactory.createShape("Square");
                myShape.showArea();
                break;

            case 4:
                myShape = ShapeFactory.createShape("Rectangle");
                myShape.showArea();
                break;
            }
        }