-6

以下のコードは私のインタビューの質問ですが、完璧にする方法がわかりません

コード:

    public class Shape
    {
        public void Rectangle(int length, int height)
        { 
            Console.Write(length * height);
        }

        public void Circle(int radius)
        {
            Console.Write(3.14 * (radius * radius));
        }
    }

何か案は?前もって感謝します

4

2 に答える 2

4

どうですか?

public abstract class Shape
{
    public abstract int CalcArea();
}

public class Rectangle : Shape
{
    public int Height { get; set; }
    public int Width { get; set; }

    public override int CalcArea()
    {
        return Height * Width;
    }
}

public class Circle : Shape
{
    public float Radius { get; set; }

    public override int CalcArea()
    {
        return Math.PI * (Radius * Radius);
    }
}
于 2013-09-13T05:59:59.310 に答える
0
  • インターフェイスShapeまたは抽象クラスを作成する

  • getArea()で抽象メソッドを宣言するShape

  • 高さと幅を持ち、半径を 持つのmakeRectangleCircleサブクラス。ShapeRectangleCircle

  • andで実装( h*w ingetArea()およびπ*r 2 inを返す)RectangleCircleRectangleCircle

于 2013-09-13T05:51:17.360 に答える