C#GetName(),GetArea()用于自定义类“Shape”及其派生类

时间:2017-06-11 10:32:15

标签: c#

我有一个任务:

有一个层次结构:“Shape” - 基类,“三角形”,“圆形”,“矩形” - 派生类“Shape”,“IsoscelesTriangle” - 派生类“Triangle”,“Square” - 派生“矩形”类。 “Shape”有方法:GetArea() - 返回几何形状的区域,GetName() - 返回几何形状的名称。对于每个派生类区域和名称可以确定。控制台程序使用名称和区域的输出消息演示多态的原理。

所以此时我的主要看起来像这样:

        Problem1_1.Shape triangle1 = new Problem1_1.Triangle("triangle1", 5, 10);
        double triangle1Area = triangle1.GetArea();
        string triangle1Name = triangle1.GetName();
        Console.WriteLine(triangle1Name, triangle1Area);

        Problem1_1.Shape isoTriangle1 = new Problem1_1.IsoscelesTriangle("iso triangle", 2, 10);
        double isoTriangle1Area = isoTriangle1.GetArea();
        string isoTriangle1Name = isoTriangle1.GetName();
        Console.WriteLine(isoTriangle1Name, isoTriangle1Area);

        Problem1_1.Shape circle1 = new Problem1_1.Circle("circle1", 5);
        double circle1Area = circle1.GetArea();
        string circle1Name = circle1.GetName();
        Console.WriteLine(circle1Name, circle1Area);

        Problem1_1.Shape rect1 = new Problem1_1.Rectangle("rectangle1", 2, 10);
        double rect1Area = rect1.GetArea();
        string rect1Name = rect1.GetName();
        Console.WriteLine(rect1Name, rect1Area);

        Problem1_1.Shape square1 = new Problem1_1.Square("sq1", 2, 3);
        double square1Area = square1.GetArea();
        string square1Name = square1.GetName();
        Console.WriteLine(square1Name, square1Area);

我的自定义类名称空间如下所示:

namespace Problem1_1
{
    public abstract class Shape
{
    protected Shape(string name)
    {
        Name = name;
    }
    public string Name { get; }

    public virtual string GetName()
    {
        return "Shape: " + Name;
    }

    public abstract double GetArea();

}

public class Triangle : Shape
{
    private double side;
    private double height;

    public Triangle(string name, double side, double height) : base (name)
    {
        this.side = side;
        this.height = height;
    }

    public override double GetArea()
    {
        double area = (side * height) / 2;
        return area;
    }
}

public class Circle : Shape
{
    private double radius;

    public Circle(string name, double radius) : base (name)
    {
        this.radius = radius;
    }

    public override double GetArea()
    {
        double area = radius * radius * Math.PI;
        return area;
    }
}

public class Rectangle : Shape
{
    private double side1;
    private double side2;

    public Rectangle(string name, double side1, double side2) : base (name)
    {
        this.side1 = side1;
        this.side2 = side2;
    }

    public override double GetArea()
    {
        double area = side1 * side2;
        return area;
    }
}

public class IsoscelesTriangle : Triangle
{

    public IsoscelesTriangle(string name, double side, double height) : base (name, side, height) { }

    public override double GetArea()
    {
        return base.GetArea();
    }
}

public class Square : Rectangle
{
    private double side1;

    public Square(string name, double side1, double side2) : base(name, side1, side2)
    {
        this.side1 = side1;
    }


    public override double GetArea()
    {
        double area = side1 * side1;
        return area;
    }
}
}

那么问题是什么。

首先,我的控制台消息没有给我这个区域。它只给我这个名字。我应该改变什么来获得两者?

其次,我不知道如何处理Square类。如果它是从矩形派生的,我怎么才能得到只有1个值来计算面积。如果我这样设置:

public Square(string name, double side1) : base(name, side1)

我收到了一个错误。

2 个答案:

答案 0 :(得分:1)

据我所知,Conosole.WriteLine()没有过载;它允许你打印两个或更多这样的变量,你需要使用格式,如, Console.WriteLine(" {0}:{1}",triangleName,triangleArea); Console.WriteLine($" {triangle1Name}:{triangle1Area}");

你能再解释一下第二个问题吗?

答案 1 :(得分:0)

为什么在构造函数中为Square的一侧传递两个值?你只需要一面来定义一个Square。

public class Square : Rectangle
{
    private double side1;

    public Square(string name, double side1) : base(name, side1, side1)
    {
        this.side1 = side1;
    }
}

然后将Square的构造函数更改为

public class Rectangle : Shape
{
    protected double side1;
    protected double side2;
    ...
}

如您所见,您还可以删除GetArea的覆盖,并让Square(Rectangle)的基类返回其计算。

我建议在Rectangle类 protected 中创建两个边变量,这样你也可以删除Square类中的变量side1

@media screen and (-ms-high-contrast: active), screen and (-ms-high-contrast: none) { background: linear-gradient($angle, $color 50%, transparent 50%); }

最后,对于输出问题,您只需要正确使用Console.WriteLine,如上所示。