Возвращает объект типа shape
public abstract class Shape { public virtual double GetArea() { return 0; } public static Shape CreateShape(string input) { //write code for this method } }
public class Square : Shape { public double Width { get; set; } public double Height { get; set; } public Square(double width, double height) { this.Width = width; this.Height = height; } public override double GetArea() { return this.Width * this.Height; } }
public class Circle : Shape { public double Radius { get; set; } public Circle(double radius) { this.Radius = radius; } public override double GetArea() { return Math.PI * this.Radius * this.Radius; } }
Что я уже пробовал:
public static Shape CreateShape(string input) { string[] val = input.Split(' '); string value = "Shape type: " + val[0] + "\nLength: " + val[1] + "\nWidth: " + val[2]; Object myshape = value; return (Shape) myshape; }
Я пробовал вышеописанное, по сути, то, что я пытаюсь сделать, - это вернуть объект в CreateShape, когда он мгновенно создается в основном классе
пример установки: форма квадрат = форма.CreateShape("квадрат 30 9");