没有合适的方法找到重写C# [英] no suitable method found to override c#

查看:884
本文介绍了没有合适的方法找到重写C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几件事情要修正这个错误,我只是似乎无法推测这一个,我将不胜感激任何帮助。该错误是在这两个三角和广场类,在三角错误是没有实现从GeometricFigure继承的抽象成员,并与广场刚错误发现重写没有合适的方法,发现重写没有合适的方法。

 命名空间ShapesDemo 
{
类节目
{
静态无效的主要(字串[] args)
{
矩形REC =新的Rectangle(8,10);
广场SQU =新广场(11,12);
三角三正=新三角(10,20);
Console.WriteLine(计算面积{0}+\\\
\\\
+计算三角为:{1}+\\\
,squ.ComputeArea(rec.Area) ,tri.ComputeArea(rec.Area));

}
}

抽象类GeometricFigure
{
公共小数_height,_width,_area;


公共小数高度
{
得到{_height; }
集合{_height =价值; }
}

公共小数宽度
{
{返回_width; }
集合{_width =价值; }
}

公共小数区
{
{返回_area; }
}

公共抽象的十进制ComputeArea();

}

类Rectangle:GeometricFigure
{
私人小数高度,宽度;

公共矩形(十进制SIDEA,十进制sideB)
{
this.height = SIDEA;
this.width = sideB;
}

公共矩形()
{
}

公众覆盖小数ComputeArea()
{
Console.WriteLine(该地区是+ _width.ToString(),_height.ToString());
返回宽*高;
}

}

类方形:长方形
{
公共广场(十进制SIDEA,十进制sideB)
{
this._width = SIDEA;
this._height = sideB;
如果(SIDEA = sideB!)
this._height = this._width;
}

公共广场(十进制XY)
{
this._width = XY;
this._height = this._width;
}

公众覆盖小数ComputeArea(十进制_area)
{返回_area = this._width * this._height; }
}

类三角:GeometricFigure
{
公共三角(INT X,int y)对
{
this.Width = X ;
this.Height = Y;
}

公众覆盖小数ComputeArea(十进制_area)
{返回_area =(this.Width * this.Height)/ 2; }
}
}


解决方案

每当你覆盖一个方法,你有相同签名的基类覆盖(有协方差和逆变例外,但这些并不适用于你的问题,所以我在这里忽略它们)。



GeometricFigure ,您必须声明

 公共抽象小数ComputeArea(); 



广场三角你有声明

 公众覆盖小数ComputeArea(十进制_area)
{
// ...
}

让我们说一些其他类包含下面的代码:

  GeometricFigure图=新三角(10,10); 
十进制面积= fig.ComputeArea();



其中 ComputeArea 将被称为? 三角没有定义 ComputeArea 不带任何参数,而且也不 GeometricFigure ,所以没有有效的 ComputeArea 打电话。其结果是,语言规范通过要求覆盖只能放在实际重写基类的方法方法,用的参数相同数量和类型不允许这种情况。由于 ComputeArea(十进制)不覆盖 ComputeArea(),编译器错误,并告诉你,你必须把在 ComputeArea()定义override关键字三角,那你不能把override关键字上 ComputeArea(十进制)



这并不是说你不能定义一个方法 ComputeArea(十进制)三角广场,但你不能将它声明为覆盖 ComputeArea() GeometricFigure


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.

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

public abstract decimal ComputeArea();

but in Square and Triangle you have the declaration

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? Triangle doesn't define a ComputeArea with no arguments, and neither does GeometricFigure, so there is no valid ComputeArea to call. 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. 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).

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.

这篇关于没有合适的方法找到重写C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆