简体   繁体   English

找不到合适的方法来覆盖c#

[英]no suitable method found to override c#

I have tried a few things to fix the error and I just can't seem to figure this one out, I would greatly appreciate any help. 我已经尝试了一些方法来修复错误,我似乎无法想出这个,我会非常感谢任何帮助。 The error is in both the Triangle and Square classes, the errors in Triangle are "does not implement inherited abstract member from GeometricFigure" and "no suitable method found to override" and Square has just the "no suitable method found to override" error. 错误在Triangle和Square类中,Triangle中的错误是“没有实现GeometricFigure的继承抽象成员”和“没有找到合适的方法来覆盖”而Square只有“找不到合适的方法来覆盖”错误。

namespace ShapesDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rec = new Rectangle(8,10);
            Square squ = new Square(11, 12);
            Triangle tri = new Triangle(10, 20);
            Console.WriteLine("Computed area is {0}" + "\n\n" + "Computed Triangle is: {1}"         + "\n", squ.ComputeArea(rec.Area), tri.ComputeArea(rec.Area));

        }
    }

    abstract class GeometricFigure
    {
        public decimal _height, _width, _area;


        public decimal Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public decimal Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public decimal Area
        {
            get { return _area; }
        }

        public abstract decimal ComputeArea();

    }

    class Rectangle : GeometricFigure
    {
        private decimal height, width;

        public Rectangle(decimal sideA, decimal sideB)
        {
            this.height = sideA;
            this.width = sideB;
        }

        public Rectangle()
        {
        }

        public override decimal ComputeArea()
        {
            Console.WriteLine("The Area is" + _width.ToString(), _height.ToString());
            return width * height;
        }

    }

    class Square : Rectangle
    {
        public Square(decimal sideA, decimal sideB)
        {
            this._width = sideA;
            this._height = sideB;
            if (sideA != sideB)
                this._height = this._width;
        }

        public Square(decimal xy)
        {
            this._width = xy;
            this._height = this._width;
        }

        public override decimal ComputeArea(decimal _area)
        { return _area = this._width * this._height; }
    }

    class Triangle : GeometricFigure
    {
        public Triangle(int x, int y)
        {
            this.Width = x;
            this.Height = y;
        }

        public override decimal ComputeArea(decimal _area)
        { return _area = (this.Width * this.Height) / 2; }
    }
}

Whenever you override a method, you have to override with the same signature as in the base class (there are exceptions for covariance and contravariance, but those don't apply to your question, so I'll ignore them here). 每当你覆盖一个方法时,你必须使用与基类相同的签名覆盖(协方差和逆变的例外,但那些不适用于你的问题,所以我会在这里忽略它们)。

In GeometricFigure , you have the declaration GeometricFigure ,您有声明

public abstract decimal ComputeArea();

but in Square and Triangle you have the declaration 但是在SquareTriangle你有声明

public override decimal ComputeArea(decimal _area)
{
    // ...
}

Let's say that some other class contained the following code: 假设其他一些类包含以下代码:

GeometricFigure fig = new Triangle(10, 10);
decimal area = fig.ComputeArea();

Which ComputeArea would be called? 会调用哪个ComputeArea Triangle doesn't define a ComputeArea with no arguments, and neither does GeometricFigure , so there is no valid ComputeArea to call. Triangle没有定义没有参数的ComputeArea ,也没有定义GeometricFigure ,因此没有有效的ComputeArea可以调用。 As a result, the language spec disallows this scenario by requiring that override only be placed on methods that actually override methods of the base class, with the same number and type of arguments. 因此,语言规范不允许这种情况,要求仅将override放在实际覆盖基类方法的方法上,并使用相同数量和类型的参数。 Since ComputeArea(decimal) doesn't override ComputeArea() , the compiler errors out and tells you that you must place the override keyword on a ComputeArea() definition in Triangle , and that you can't put the override keyword on ComputeArea(decimal) . 由于ComputeArea(decimal)不会覆盖ComputeArea() ,编译器会输出错误并告诉您必须将override关键字放在TriangleComputeArea()定义中,并且不能将override关键字放在ComputeArea(decimal)

That's not to say that you can't define a method ComputeArea(decimal) on Triangle and Square , but you can't declare it as overriding ComputeArea() in GeometricFigure . 这并不是说您无法在TriangleSquare上定义ComputeArea(decimal)方法,但您不能将其声明为覆盖GeometricFigure ComputeArea()

在square和triangle类中,需要从ComputeArea()中删除method参数,使其与基类的签名匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM