簡體   English   中英

找不到合適的方法來覆蓋c#

[英]no suitable method found to override c#

我已經嘗試了一些方法來修復錯誤,我似乎無法想出這個,我會非常感謝任何幫助。 錯誤在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; }
    }
}

每當你覆蓋一個方法時,你必須使用與基類相同的簽名覆蓋(協方差和逆變的例外,但那些不適用於你的問題,所以我會在這里忽略它們)。

GeometricFigure ,您有聲明

public abstract decimal ComputeArea();

但是在SquareTriangle你有聲明

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

假設其他一些類包含以下代碼:

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

會調用哪個ComputeArea Triangle沒有定義沒有參數的ComputeArea ,也沒有定義GeometricFigure ,因此沒有有效的ComputeArea可以調用。 因此,語言規范不允許這種情況,要求僅將override放在實際覆蓋基類方法的方法上,並使用相同數量和類型的參數。 由於ComputeArea(decimal)不會覆蓋ComputeArea() ,編譯器會輸出錯誤並告訴您必須將override關鍵字放在TriangleComputeArea()定義中,並且不能將override關鍵字放在ComputeArea(decimal)

這並不是說您無法在TriangleSquare上定義ComputeArea(decimal)方法,但您不能將其聲明為覆蓋GeometricFigure ComputeArea()

在square和triangle類中,需要從ComputeArea()中刪除method參數,使其與基類的簽名匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM