C#类没有实现继承的抽象成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1916918/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:49:37  来源:igfitidea点击:

C# class does not implement inherited abstract member

c#

提问by solidsn2004

I am sorry if I am asking something stupid but I am completely a newbie in C# and ASP.NET. I am having an error in my code and I don't understand it. I am working on Visual Studio 2008.

如果我问一些愚蠢的问题,我很抱歉,但我完全是 C# 和 ASP.NET 的新手。我的代码中有错误,我不明白。我正在使用 Visual Studio 2008。

In this line of code:

在这行代码中:

public class SQLFAQProvider : DBFAQProvider

I am getting this error:

我收到此错误:

Moby.Commerce.DataAccess.FAQ.SQLFAQProviderdoes not implement inherited abstract member Moby.Commerce.DataAccess.FAQDBFAQProvider.DeleteFAQbyID(int)

Moby.Commerce.DataAccess.FAQ.SQLFAQProvider不实现继承的抽象成员 Moby.Commerce.DataAccess.FAQDBFAQProvider.DeleteFAQbyID(int)

When I go to DBFAQProviderthe error is in this line of code:

当我转到DBFAQProvider错误在这行代码中时:

public abstract DBFAQ DeleteFAQbyID(int fAQID);

What should I change to correct it?

我应该改变什么来纠正它?

采纳答案by Quintin Robinson

First thought would be implement the abstract member in the inherited class ala:

首先想到的是在继承的类ala中实现抽象成员:

public class SQLFAQProvider : DBFAQProvider
{
    public override DBFAQ DeleteFAQbyID(int fAQID)
    {
        //TODO: Real Code
        return null;
    }
}

回答by Matthew Sposato

Your subclass (SQLFAQProvider) must provide implementation code for the method DeleteFAQbyID because the parent class (DBFAQProvider) did not.

您的子类 (SQLFAQProvider) 必须为方法 DeleteFAQbyID 提供实现代码,因为父类 (DBFAQProvider) 没有。

回答by Jon Skeet

Implement the DeleteFAQbyIDmethod in your derived class:

DeleteFAQbyID在派生类中实现该方法:

public override DBFAQ DeleteFAQbyID(int fAQID)
{
    // Put your code here
}

The point of an abstract method is to say (in the abstract base class), "I want to make sure that this method is available in every concrete class deriving from me" - it's up to you to provide the implementation. It's a bit like a method in an interface.

抽象方法的重点是说(在抽象基类中),“我想确保这个方法在从我派生的每个具体类中都可用” - 由您来提供实现。它有点像接口中的方法。

回答by Ed S.

When a class inherits from an abstract class it must implement all abstract methods defined by said class. This is the class interface, the abstract methods can be thought of as pure virtual functions, i.e., functions that must be implemented by descended classes but do not make sense to be implemented in the base class.

当一个类从一个抽象类继承时,它必须实现该类定义的所有抽象方法。这是类接口,抽象方法可以认为是纯虚函数,即必须由后代类实现但在基类中实现没有意义的函数。

回答by Chris Cudmore

Your subclass needs to explicitly implement that particular method.

您的子类需要显式实现该特定方法。

If you have no idea how to do it, then at least do:

如果你不知道怎么做,那么至少做:

public override DBFAQ DeleteFAQbyID(int fAQID)
{
    throw new NotImplementedException("This isn't done yet");
}

回答by SLaks

Because your SQLFAQProviderclass isn't abstract, it has to implement every abstractmethod that it inherits.

因为您的SQLFAQProvider类不是abstract,所以它必须实现abstract它继承的每个方法。

To fix this, implement the DeleteFAQbyIDmethod in SQLFAQProvider, like this:

要解决此问题,请实现 中的DeleteFAQbyID方法SQLFAQProvider,如下所示:

public override DBFAQ DeleteFAQbyID(int fAQID) {
    //Do something
}

Alternatively, you could make your SQLFAQProviderclass abstractby changing its declaration to public abstract class SQLFAQProvider.

或者,你可以让你的SQLFAQProviderabstract通过改变它的声明public abstract class SQLFAQProvider

回答by LBushkin

When you inherit from a class in C#, you are required to implement all methods marked as abstractunless your class is itself marked as abstract. Abstract classes are ones that cannot be directly instantiated at runtime because they don't fully implement all of the required methods that the base class(es) say must exist.

当您从 C# 中的类继承时,您需要实现所有标记为的方法,abstract除非您的类本身标记为abstract. 抽象类是不能在运行时直接实例化的类,因为它们没有完全实现基类所说必须存在的所有必需方法。

Abstract methods are a mechanism that allows a class to indicate that a particular method must "eventually" exist - without having to provide an implementation at that point. You typically use abstract classes when you cannot or don't want to dictate what a particular implementation should do, but you need to pre-define a method that you will eventually rely on.

抽象方法是一种机制,允许类指示特定方法必须“最终”存在 - 无需在此时提供实现。当您不能或不想指定特定实现应该做什么时,通常会使用抽象类,但您需要预定义最终将依赖的方法。

To fix your problem either mark your class as abstract (with the expectation that another level of inheritance will fill in the missing pieces) or implement DeleteFAQbyId() in your class:

要解决您的问题,请将您的类标记为抽象类(期望另一级继承将填补缺失的部分)或在您的类中实现 DeleteFAQbyId():

public DBFAQ DeleteFAQbyID(int fAQID)
{
    // write appropriate implementation here or:
    throw new NotImplementedException();
    // or possibly NotSupportedException() if the operation should can't ever be supported.
}

回答by adithiyaa

In the abstract class use an entity property like IsValid. Make it return the abstract method that you want to override in the derived class.

在抽象类中使用像IsValid. 使其返回您要在派生类中覆盖的抽象方法。

In abstract base class:

在抽象基类中:

public bool IsValid
{
    get
    {
        return  DeleteFAQbyID();
    }
}

public abstract bool DeleteFAQbyID();

In the derived class now it will override the abstract class' method.

现在在派生类中它将覆盖抽象类的方法。