1

我的要求是使用形状的名称使用方法中的尺寸绘制该形状Draw('rectangle', 'l:10,w:20');

  1. 应该根据形状的类型验证尺寸。
  2. 可以重构这些类以添加更多类或更改层次结构。
  3. 不应使用像反射这样的运行时检查。这个问题只需要通过类设计来解决。
  4. 不要在客户端方法中使用if-elseorswitch语句Draw

要求:

public static void main()
{
    // Provide the shape and it's dimensions
    Draw('rectangle', 'l:10,w:20');
    Draw('circle', 'r:15');
}

我创建了以下类。我通过创建两个类层次结构来考虑低(松散)耦合和高内聚性,这样它们就可以自行增长。我保留了绘制到一个类并为另一个类生成维度的责任。

我的问题是关于创建这些对象并相互交互以实现我的要求。

public abstract class Shape()
{
    Dimension dimension;
    public void abstract SetDimentions(Dimension dimension);
    public void abstract Draw()
}

public void Rectangle()
{
    void override SetDimensions(RectangleDimension dimension)
    {
    }

    void override Draw()
    {
        // Use the 'dimention' to draw
    }
}

public void Circle()
{
    void override SetDimensions(CircleDimension dimension)
    {
    }

    void override Draw()
    {
        // Use the 'dimention' to draw
    }
}

public class RectangleDimension
{
    public int length {get; set; }
    public int width { get; set; }
}

public class CircleDimension
{
    public int circle { get; set; }
}
4

1 回答 1

1

您将需要在您使用的任何 OOP 技术中使用反射。你收到一个String"circle",你需要用那个名字调用一个方法。

这就是你在Java中如何做到这一点,这就是你如何在C#中做到这一点。

于 2017-03-22T12:26:53.307 回答