找不到合适的方法来覆盖 c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9458572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
no suitable method found to override c#
提问by John Joseph
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; }
}
}
采纳答案by Adam Mihalcin
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 Squareand Triangleyou have the declaration
但在Square和Triangle你的声明
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 ComputeAreawould be called? Triangledoesn't define a ComputeAreawith no arguments, and neither does GeometricFigure, so there is no valid ComputeAreato call. As a result, the language spec disallows this scenario by requiring that overrideonly 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).
哪个ComputeArea会被调用? Triangle没有定义ComputeArea没有参数的a ,也没有定义GeometricFigure,所以没有有效ComputeArea的调用。因此,语言规范要求override仅将其放置在实际覆盖基类方法的方法上,并具有相同数量和类型的参数,从而不允许这种情况。由于ComputeArea(decimal)没有 override ComputeArea(),编译器会出错并告诉您必须将 override 关键字放在 中的ComputeArea()定义上Triangle,并且不能将 override 关键字放在 上ComputeArea(decimal)。
That's not to say that you can't define a method ComputeArea(decimal)on Triangleand Square, but you can't declare it as overriding ComputeArea()in GeometricFigure.
这并不是说您不能ComputeArea(decimal)在Triangleand上定义方法Square,而是不能ComputeArea()在GeometricFigure.
回答by Erik Dietrich
In your square and triangle classes, you need to remove the method parameter from ComputeArea() so that it matches the signature of the base class.
在您的正方形和三角形类中,您需要从 ComputeArea() 中删除方法参数,使其与基类的签名相匹配。

