static public Shape CreateShape(string type) { Shape shape = null; try { if (type.Equals("triangle")) { Console.WriteLine("请输入三角形的三条边:"); double a = Convert.ToDouble(Console.ReadLine()); double b = Convert.ToDouble(Console.ReadLine()); double c = Convert.ToDouble(Console.ReadLine()); shape = new Triangle(a, b, c); } if (type.Equals("circular")) { Console.WriteLine("请输入圆形的半径:"); double radius = Convert.ToDouble(Console.ReadLine()); shape = new Circular(radius); } if (type.Equals("square")) { Console.WriteLine("请输入正方形的边长:"); double sideLength = Convert.ToDouble(Console.ReadLine()); shape = new Square(sideLength); } if (type.Equals("rectangle")) { Console.WriteLine("请输入矩形的长和宽:"); double length = Convert.ToDouble(Console.ReadLine()); double width = Convert.ToDouble(Console.ReadLine()); shape = new Rectangle(length, width); } } catch (Exception e) { Console.WriteLine(e.Message); } return(shape); }
//静态工厂方法 public static Sharp GetSharp(String type, double a, double b) { Sharp sharp = null; if (type.Equals("三角形")) { sharp = new Triangle(a); } else if (type.Equals("圆形")) { sharp = new Circular(a); } else if (type.Equals("正方形")) { sharp = new Square(a); } else if (type.Equals("长方形")) { sharp = new Rectangle(a, b); } return(sharp); }
public static Graphical CreatGraphical(string type) { Graphical n = null; switch (type) { case "长方形": n = new Rctangle(); break; case "正方形": n = new Square(); break; case "圆形": n = new Circular(); break; case "三角形": n = new Triangle(); break; } return(n); }