简体   繁体   English

C#“找不到合适的方法来覆盖。” - 但是有一个

[英]C# “No suitable method found to override.” — but there is one

I'm having trouble overriding the method of a parent class in C#. 我无法在C#中覆盖父类的方法。 The parent class is setup like so: 父类的设置如下:

public class Base {
    public Base(Game1 game)
    {
        this.game = game;
    }

    public virtual void Draw()
    {
    }
}

...And the child class: ......和孩子班:

public class Ext : Base {
    public Ext(Game1 game) : base(game)
    {
    }

    public override void Draw(SpriteBatch batch)
    {
    }
}

I know I've successfully overridden a parent method in the past, and right now I'm probably overlooking something incredibly simple... what is it? 我知道我过去成功地覆盖了一个父方法,现在我可能忽略了一些非常简单的东西......它是什么?

EDIT: That was actually a typo: in the actual script, Ext does derive from Base. 编辑:这实际上是一个错字:在实际的脚本中,Ext确实来自Base。 The problem still persists. 问题仍然存在。 Thanks for the quick answers, though. 但是,谢谢您的快速解答。 :) :)

Your code as given (after the edit) compiles fine, so something else is wrong that isn't in what you posted. 你给出的代码(在编辑之后)编译得很好,所以其他错误不是你发布的内容。

Some things to check, is everything public? 有些事要检查,一切都是公开的吗? That includes both the class and the method. 这包括类和方法。

Overload with different parameters? 有不同参数的过载?

Are you sure that Base is the class you think it is? 你确定Base是你认为的类吗? Ie is there another class by the same name that it's actually referencing? 即它是否有另一个同名的类,它实际上是引用它?

Edit: 编辑:

To answer the question in your comment, you can't override a method with different parameters, nor is there a need to. 要回答评论中的问题,您不能使用不同的参数覆盖方法,也不需要。 You can create a new method (with the new parameter) without the override keyword and it will work just fine. 您可以创建一个没有override关键字的新方法(使用新参数),它可以正常工作。

If your intention is to prohibit calling of the base method without the parameter you can mark the method as protected instead of public . 如果您的目的是禁止在没有参数的情况下调用base方法,则可以将方法标记为protected而不是public That way it can only be called from classes that inherit from Base 这样它只能从继承自Base类中调用

You're not inheriting from your base class: 你不是从你的基类继承的:

public class Ext : Base {
    // constructor

    public override void Draw()
    {
    }
}

The signature of your methods is different. 您的方法的签名是不同的。 But to override a method the signature must be identical. 但是要覆盖方法,签名必须相同。

In your case the base class version has no parameters, the derived version has one parameter. 在您的情况下,基类版本没有参数,派生版本有一个参数。

So what you're trying to do doesn't make much sense. 所以你要做的事情没有多大意义。 The purpose is that if somebody calls the base function on a variable that has the static type Base but the type Ext at runtime the call will run the Ext version. 目的是如果有人在具有静态类型Base但在运行时类型为Ext的变量上调用基函数,则调用将运行Ext版本。 That's obviously not possible with different parameter counts. 对于不同的参数计数,这显然是不可能的。

Perhaps you don't want to override at all? 也许你根本不想覆盖?

I ran into this issue only to discover a disconnect in one of my library objects. 我遇到这个问题只是为了发现我的一个库对象断开连接。 For some reason the project was copying the dll from the old path and not from my development path with the changes. 出于某种原因,该项目是从旧路径复制dll而不是从我的开发路径复制更改。 Keep an eye on what dll's are being copied when you compile. 在编译时要密切注意复制的dll。

Ext需要继承base,所以在你的定义中它应该说:

public class Ext : Base { //...

You cannot override a function with different parameters, only you are allowed to change the functionality of the overridden method. 您不能使用不同的参数覆盖函数,只允许您更改被覆盖的方法的功能。

//Compiler Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

//Overriding & overloading

namespace polymorpism
{
    public class Program
    {
        public static void Main(string[] args)
        {

            drowButn calobj = new drowButn();

            BtnOverride calobjOvrid = new BtnOverride();

            Console.WriteLine(calobj.btn(5.2, 6.6).ToString());

            //btn has compleately overrided inside this calobjOvrid object
            Console.WriteLine(calobjOvrid.btn(5.2, 6.6).ToString());
            //the overloaded function
            Console.WriteLine(calobjOvrid.btn(new double[] { 5.2, 6.6 }).ToString());

            Console.ReadKey();
        }
    }

    public class drowButn
    {

        //same add function overloading to add double type field inputs
        public virtual double btn(double num1, double num2)
        {

            return (num1 + num2);

        }


    }

    public class BtnOverride : drowButn
    {

        //same add function overrided and change its functionality 
        //(this will compleately replace the base class function
        public override double btn(double num1, double num2)
        {
            //do compleatly diffarant function then the base class
            return (num1 * num2);

        }

        //same function overloaded (no override keyword used) 
        // this will not effect the base class function

        public double btn(double[] num)
        {
            double cal = 0;

            foreach (double elmnt in num)
            {

                cal += elmnt;
            }
            return cal;

        }
    }
}

You have forgotten to derive from Base 你忘记了从Base派生出来

public class Ext : Base
                 ^^^^^^        

我没有看到类Ext是从Base派生的。

您需要从基类继承。

确保您让子类显式继承父类:

public class Ext : Base { // stuff }

I ran into a similar situation with code that WAS working , then was not. 我遇到类似的情况,代码是WAS工作,然后不是。

Turned while dragging / dropping code within a file, I moved an object into another set of braces. 在文件中拖放代码时转动,我将一个对象移动到另一组括号中。 Took longer to figure out than I care to admit. 花了很长时间才弄清楚,而不是我承认。

Bit once I move the code back into its proper place, the error resolved. 比特一旦我将代码移回适当的位置,错误就解决了。

Probably your base class is a different one with the same name. 可能你的基类是一个具有相同名称的基类。 Check by right click on the extended class and go to the definition. 通过右键单击扩展类进行检查并转到定义。 Check if the class is correct one. 检查班级是否正确。

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

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