예제 #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)
        {
            var test1 = ShapeFactory.GetResult("Round");
            var test2 = ShapeFactory.GetResult("Square");
            var test3 = ShapeFactory.GetResult("Triangle");
            var test4 = ShapeFactory.GetResult("Rectangle");

            test1.Numradius = 10;
            test2.Numside   = 4;
            test3.Numbase   = 5;
            test3.Numheight = 3;
            test4.Numlength = 8;
            test4.Numwidth  = 3;
            Console.WriteLine("圆面积为:" + test1.Result());
            Console.WriteLine("正方形面积为:" + test2.Result());
            Console.WriteLine("三角形面积为:" + test3.Result());
            Console.WriteLine("长方形面积为:" + test4.Result());
        }
예제 #3
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;
            }
        }