简体   繁体   中英

c#, force method implementation in child class

I have this setting:

public class BaseElement
{
    public BaseElement() { }
    public virtual void method01()
    {
        Console.WriteLine("class BaseElement method01");
    }
}
public class Element01 : BaseElement
{
    public Element01() { }
    public override void method01()
    {
        Console.WriteLine("class Element01 method01");
    }
}
public class Element02 : BaseElement
{
    public Element02() { }
    public override void method01()
    {
        Console.WriteLine("class Element02 method01");
    }
}

Now I want to force class Element01/Element02/Element03 and so on to implement method01. I think the correct way is to use an interface. But due to the fact that Element01/Element02/Element03 and so on inherit from BaseElement there is no need to implement method01 in Element01/Element02/Element03 and so on because it is already implemented in BaseElement. But I want to force Element01/Element02/Element03 and so on to implement a specific method01 in each of these classes. On the other hand I need method01 in BaseElement also to address Element01/Element02/Element03 and so on as BaseElement. What would be the best way to do this?

(It has nothing to do with the difference between abstract and virtual functions. I need a specific arrangement of either an interface or an abstract class.)

If I understand the question correctly you have some default behavior in base class and you need to force to implement some logic in derived class you can use Inversion of Control principle and Template method Design pattern you have to make another internal method like method01Int and it will be abstract and all derived classes should implement it for example:

 public abstract class BaseElement
{
    public BaseElement() { }
    public  void method01()
    {
        Console.WriteLine("class BaseElement method01");
        method01Int();
    }

    protected abstract void method01Int();
}
public class Element01 : BaseElement
{
    public Element01() { }
    protected override void method01Int()
    {
        Console.WriteLine("class Element01 method01");
    }
}
public class Element02 : BaseElement
{
    public Element02() { }
    protected override void method01Int()
    {
        Console.WriteLine("class Element02 method01");
    }
}

even if in derived class don't have some logic you can implement like this

    public class Element03 : BaseElement
{
    public Element03() { }
    protected override void method01Int()
    {

    }
}

Create two interface . One for base class and another for sub classes. And implement method01() explicitly in BaseElement class. Hope it helps.

class Program
{
    static void Main(string[] args)
    {
        IBase obj = new BaseElement();
        IElement obj1 = new Element01();
        obj.method01();//call base
        obj1.method01();

        Console.ReadLine();
    }
}
public interface IBase
{
    void method01();
}
public interface IElement
{
    void method01();
}
public class BaseElement : IBase
{
    public BaseElement() { }
    void IBase.method01()
    {
        Console.WriteLine("class BaseElement method01");
    }
}
public class Element01 : BaseElement, IElement
{
    public Element01() { }
    public void method01()
    {
        Console.WriteLine("class Element02 method01");
    }

}
public class Element02 : BaseElement, IElement
{
    public Element02() { }
    public void method01()
    {
        Console.WriteLine("class Element02 method01");
    }
}

Use a MiddleElement:

public class BaseElement
{
    public BaseElement() { }
    public virtual void method01()
    {
        Console.WriteLine("class BaseElement method01");
    }
}
public abstract class MiddleElement: BaseElement
{
    public abstract override void method01();
};

public class Element01 : MiddleElement
{
    public Element01() { }
    public override void method01()
    {
        Console.WriteLine("class Element02 method01");
    }
}
public class Element02 : MiddleElement
{
    public Element02() { }
    public override void method01()
    {
        Console.WriteLine("class Element02 method01");
    }
}

That being said, I would like to understand why you have these requirements. There may be a simpler approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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