static void Main(string[] args) { Console.WriteLine("圆形=circular"); Console.WriteLine("三角形=triangle"); Console.WriteLine("正方形=square"); Console.WriteLine("矩形=rectangle"); Console.WriteLine("请输入你想要使用的图形:"); string type = Console.ReadLine(); //ShapeFactory factory = new ShapeFactory(); try { Shape shape = ShapeFactory.CreateShape(type);//工厂的静态成员函数 Console.WriteLine("面积为:" + shape.Area()); } catch { Console.WriteLine("您的输入有误!"); } }
static void Main(string[] args) { Shape shape1; shape1 = ShapeFactory.getShape("triangle"); shape1.display(); Shape shape2; shape2 = ShapeFactory.getShape("circle"); shape2.display(); Shape shape3; shape3 = ShapeFactory.getShape("sqare"); shape3.display(); Shape shape4; shape4 = ShapeFactory.getShape("rectangle"); shape4.display(); Console.WriteLine("按任意键以结束"); Console.ReadKey(); }
static void Main(string[] args) { try { IShape triangle = ShapeFactory.GetShape("triangle", 3, 4, 5); triangle.GetArea(); } catch (Exception e) { Console.WriteLine("Unsupported graphics"); } try { IShape circle = ShapeFactory.GetShape("circle", 3); circle.GetArea(); } catch (Exception e) { Console.WriteLine("Unsupported graphics"); } try { IShape square = ShapeFactory.GetShape("square", 3); square.GetArea(); } catch (Exception e) { Console.WriteLine("Unsupported graphics"); } try { IShape rectangle = ShapeFactory.GetShape("rectangle", 3, 4); rectangle.GetArea(); } catch (Exception e) { Console.WriteLine("Unsupported graphics"); } }