static void Main(string[] args) { bool endApp = false; while (!endApp) { Console.WriteLine("请选择构造的形状:"); Console.WriteLine("\tc - 圆"); Console.WriteLine("\tr - 长方形"); Console.WriteLine("\ts - 正方形"); Console.WriteLine("\tt - 三角形"); string op = Console.ReadLine(); Graphical graphical = GraphicalFactory.CreateGraphical(op); if (graphical.IsLegal()) { graphical.area = graphical.AreaGraphical(); graphical.perimeter = graphical.PerimeterGraphical(); Console.WriteLine("该图形的面积为:" + graphical.area); Console.WriteLine("该图形的周长为:" + graphical.perimeter); } else { Console.WriteLine("\t输入图形不合法"); } Console.WriteLine("按“n”结束程序,或者其他键继续计算"); if (Console.ReadLine() == "n") { endApp = true; } } }
public static Graphical CreateGraphical(String shape) { Graphical open = null; switch (shape) { case "c": open = new Circle(); break; case "r": open = new Rectangle(); break; case "s": open = new Square(); break; case "t": open = new Triangle(); break; } return(open); }