简体   繁体   English

c#,子类中的强制方法实现

[英]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. 现在,我想强制类Element01 / Element02 / Element03等来实现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. 但是由于Element01 / Element02 / Element03等继承自BaseElement,因此无需在Element01 / Element02 / Element03等中实现method01,因为它已在BaseElement中实现。 But I want to force Element01/Element02/Element03 and so on to implement a specific method01 in each of these classes. 但是我想强制Element01 / Element02 / Element03等在每个这些类中实现特定的method01。 On the other hand I need method01 in BaseElement also to address Element01/Element02/Element03 and so on as BaseElement. 另一方面,我还需要BaseElement中的method01来处理Element01 / Element02 / Element03等,以作为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: 如果我正确理解了这个问题,则您在基类中有一些默认行为,并且需要强制在派生类中实现一些逻辑,则可以使用控制反转原理和模板方法设计模式,您必须制作另一个内部方法,例如method01Int,它将是抽象的,所有派生类都应实现它,例如:

 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. 并在BaseElement类中显式实现method01() 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: 使用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. 可能有一种更简单的方法。

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

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