错误消息:未实现继承的抽象成员

时间:2017-04-25 13:05:11

标签: c# inheritance abstract-class abstract

小心,因为我是C#的新手!所以我有一个常规类Cube,它继承自抽象类Shape3D,后者继承自抽象类Shape2D

Shape2D提供了两个抽象属性:Area& PerimeterCube构造函数调用基类构造函数,并设置新对象的长度,高度和宽度。

出于某种原因,我从Cube收到此错误:

Error: 'Cube' does not implement inherited abstract member 'Shape2D.Area.get'
Error: 'Cube' does not implement inherited abstract member 'Shape2D.Perimeter.get'

这就是Shape2D的样子:

abstract class Shape2D : Shape
{
    private double _length, _width;
    public double Length{..}
    public double Width{..}

    public abstract double Area{ get; }

    public abstract double Perimeter{ get; }

    protected Shape2D(ShapeType shapeType, double length, double width) 
    : base(shapeType)
    {
        _length = length;
        _width = width;
    }

    public override string ToString(){..}
}

Shape 3D

abstract class Shape3D : Shape2D
{
    protected Shape2D _baseShape;
    private double _height;

    public double Height{..}
    public virtual double MantelArea{..}
    public virtual double TotalSurfaceArea{..}
    public virtual double Volume{..}

    protected Shape3D(ShapeType shapeType, Shape2D baseShape, double height)
    : base(shapeType, height, height)
    {
        _baseShape = baseShape;
        _height = height;
    }

    public override string ToString(){..}
}

Cube

class Cube : Shape3D --> This returns inherited error.
{
    ...
}

我已经谷歌了,我没有找到任何有用的东西。为什么我的常规类不从抽象类继承抽象成员?有人可以指导我吗?

1 个答案:

答案 0 :(得分:0)

因为你的2d类是带有两个抽象属性的抽象类,所以你需要在你的具体类中实现它们,如下所示。

快速编辑以回答您的问题 - 属性是抽象的,这就是为什么它似乎不会继承'他们。在进行具体实现时,您需要实现任何抽象属性。如果你认为你应该从2dShape上的摘要中改变它们,那么它们应该保持抽象,因为不同的形状会有不同的公式来计算周长和周长。基于长度和面积的面积宽度。

class Cube : Shape3D
{
   public override double Area{ get; }

    public override double Perimeter{ get; }

}