How to force sub classes to implement a method

2019-02-11 10:39发布

I am creating an object structure and I want all sub classes of the base to be forced to implement a method.

The only ways I could think of doing it were:

  1. An abstract class - Would work but the base class has some useful helper functions that get used by some of the sub classes.

  2. An interface - If applied to just the base class then the sub classes don't have to implement the function only the base class does.

Is this even possible?

N.B. This is a .NET 2 app.

4条回答
SAY GOODBYE
2楼-- · 2019-02-11 10:44

An abstract class - Would work but the base class has some useful helper functions that get used by some of the sub classe

An abstract class doesn't require all functions it provides to be abstract.

abstract class Base {
    public void Foo() {} // Ordinary method
    public virtual void Bar() {} // Can be overridden
    public abstract void Xyz(); // This one *must* be overridden
}

Note that if you replace public with protected, the marked method will be only visible to base classes and subclasses.

查看更多
等我变得足够好
3楼-- · 2019-02-11 10:59

You can have abstract methods in a class with other methods that are implemented. The advantage over an interface is that you can include some code with your class and have the new object be forced to fill in the details for the abstract methods.

public abstract class YourClass
{
    // Your class implementation

    public abstract void DoSomething(int x, int y);

    public void DoSomethingElse(int a, string b)
    {
        // You can implement this here
    }
}
查看更多
Bombasti
4楼-- · 2019-02-11 11:00

Yes, and if all the classes you need to do this for are logically subclasses of an existing abstract base class, then add an abstract method to the base class... This is better than an interface because it allows you to add implementation later (by changing abstract base class method to virtual method with a default implementation), if/when it turns out that, say, eight of ten derived classes will have the same implementation, and say, only two of them differ...

EDIT: (based on thread in comments below) The base class must be declared as abstract to do this... You can't have an abstract method in a non-abstract class because a non-abstract class can be instantiated, and if an instance of it was created, there wouldbe NO implementation for that method. So this is illegal. By declaring the base as abstract, you inhibit instantiation of the class. Then, only non-abstract derived classes can be instantiated, where, (because the base method is abstract) you MUST add an implementation for that method.

查看更多
疯言疯语
5楼-- · 2019-02-11 11:08

An interface - If applied to just the base class then the sub classes don't have to implement the function only the base class does.

This is not entirely correct. If the base class is abstract, you can mark methods that belong to the interface as abstract, and force the implementation in the subclasses.

That brings an option you didn't mention: to use both. You have an IFoo interface, and a FooBase abstract base class the implements it, or part of it. This provides subclasses with a "default" implementation of the interface (or part of it), and also lets you inherit from something else and still implement the interface, or if you want to implement the interface but not inherit the base class implementation. An example might help:

// Your interface
interface IFoo { void A(); void B; }

// A "default" implementation of that interface
abstract class FooBase : IFoo
{
    public abstract void A();

    public void B()
    {
        Console.WriteLine("B");
    }
}

// A class that implements IFoo by reusing FooBase partial implementation
class Foo : FooBase
{
    public override void A()
    {
        Console.WriteLine("A");
    }
}

// This is a different class you may want to inherit from
class Bar
{
    public void C()
    {
        Console.WriteLine("C");
    }
}

// A class that inherits from Bar and implements IFoo
class FooBar : Bar, IFoo
{
    public void A()
    {
        Console.WriteLine("Foobar.A");
    }
    public void B()
    {
        Console.WriteLine("Foobar.B");
    }
}
查看更多
登录 后发表回答