c# genéricos y herencia

I'm getting a compilation error on following code:

public abstract class DbHandler<T>
{
    public abstract bool Save(T obj);
    ...
}

and its implementing class:

 public class SpaghettiTableDb : DbHandler<SpaghettiTable>
 {
    public bool Save(SpaghettiTable obj)
    {
        return false;
    }
    ...
 }

El error es:

'SpaghettiTableDb' does not implement inherited abstract member 'SeatPicker.DbHandler<SeatPicker.SpaghettiTable>.Save(SeatPicker.SpaghettiTable)'

But I think it does, so I'm not sure why I'm receiving this error.

(SpaghettiTable is just a class with some properties, nothing more)

¿Alguna ayuda?

preguntado el 25 de agosto de 12 a las 15:08

Have you cleaned and rebuilt your solution? -

Visual Studio does have intellisense - so try hovering over the base class with your mouse, you'll see a little drop down, click on that and select "Implement ...". It will define it for you. -

As an aside: plugins like ReSharper are very useful in cases like this, they can generate the correct boilerplate for you. -

@millimoose vanilla Visual Studio can auto-generate the boiler plate code as well. -

2 Respuestas

Necesitas usar el override keyword. Otherwise you're not implementing the abstract base class and just creating a "new" separate method on the subclass.

public override bool Save(SpaghettiTable obj)
{
    return false;
}

Piensa en abstract methods just like virtual methods that you override. The only difference is that you fuerza subclasses to provide an implementation of that method whereas virtual methods provide their own implementation that subclasses can opcionalmente override with their own implementation.

EDIT: Additionally, if you want to make your life easier in Visual Studio, you can right-click (or ctrl+.) on the inheritance declaration and choose "implement abstract class" (or something like that, I don't have VS with me right now) which will automatically create all the overridden methods for you.

public class SpaghettiTableDb : DbHandler<SpaghettiTable> //right-click on "DbHandler"

Alternatively, in the empty code space within your class, you can start typing "override", then the IntelliSense will list all overridable members from the base class and when you pick one it will automatically write a default implementation for you.

EDIT: Just to extend on what you have in your code, without the override keyword, you are creating a new method that belongs to your subclass and not overriding the base class. When you call that method but from the context of using the base class, it won't call your subclass implementation since it doesn't override the base method.

Consider the following classes. (I'm using virtual instead of abstract just so it compiles and have it simpler)

public class BaseClass
{
    public virtual void Print()
    {
        Console.WriteLine("base print");
    }

    public virtual void AnotherPrint()
    {
        Console.WriteLine("base another print");
    }
}

public class SubClass : BaseClass
{
    public override void Print()
    {
        Console.WriteLine("sub print");
    }

    public void AnotherPrint()
    {
        Console.WriteLine("sub another print");
    }
}

Tenga en cuenta que SubClass.AnotherPrintno anular BaseClass.AnotherPrint.

And when you use code like:

SubClass mySub = new SubClass();

mySub.Print(); //sub print
mySub.AnotherPrint(); //sub another print

BaseClass myBase = mySub;

myBase.Print(); //sub print
myBase.AnotherPrint(); //base another print

Note that through the code, mySub y myBase ambos apuntan al mismo objeto, but one is typed as SubClass y el otro como BaseClass. Cuando el tiempo de ejecución llama myBase.Print(), it can easily check the inheritance of the classes and see that SubClass ha anulado la Print method and calls the SubClass implementation. However, since SubClass.AnotherPrint wasn't explicitly marked with override, the runtime/compiler considers that to be a método completamente diferente with no link to the BaseClass.AnotherPrint method. Thus the runtime sticks with the base class implementation. When your instance is typed as the SubClass though, the compiler does see that you're pointing to that new method and essentially not to the base implementation.

Respondido 25 ago 12, 16:08

@StevenLemmens You're welcome! I just added some more info about what's going on behind the scenes if you'd like to read up on it. - chris sinclair

Necesitas usar el override keyword when implementing abstract methods or overriding virtual methods.

public override bool Save(SpaghettiTable obj)
{
    return false;
}

Respondido 25 ago 12, 15:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.