Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 强制继承类调用基帮助器方法_C#_Oop - Fatal编程技术网

C# 强制继承类调用基帮助器方法

C# 强制继承类调用基帮助器方法,c#,oop,C#,Oop,我有一个基类,它包含一个helper方法。如何强制继承的类调用此helper方法?有没有一种方法可以警告使用者必须调用GenerateId?我是否应该将此助手方法作为基本构造函数逻辑的一部分 例如: public class FooBar { public int GenerateId(string productCode) { //..some logic to return an integer; } } public class AnotherFooB

我有一个基类,它包含一个helper方法。如何强制继承的类调用此helper方法?有没有一种方法可以警告使用者必须调用GenerateId?我是否应该将此助手方法作为基本构造函数逻辑的一部分

例如:

public class FooBar
{
    public int GenerateId(string productCode)
    {
       //..some logic to return an integer;
    }
}
public class AnotherFooBar : FooBar
{
    public void Save()
    {
       var fooBarId = this.GenerateId("myproduct");
       //fooBarId will be used in this code block
    }
}

不能对重写的方法强制执行任何内容。你从错误的角度看待这件事

请参阅有关此操作的正确方法

基本上,如果基类需要在每次调用重写时执行特定代码,则应仅重写基类方法的一部分,如下所示:

class A
{
   void MethodOne()
   {
      //Here you perform your obligatory logic.

      //Then, call the overridable logic.
      MethodOneCore();
   }

   virtual void MethodOneCore()
   {
      //Here you perform overridable logic.
   }
}

class B: A
{
   override void MethodOneCore()
   {
      //Here you override the "core" logic, while keeping the obligatory logic intact.
   }
}

这就是你要找的吗?你的问题不是很清楚

public class FooBar
{
    public abstract int GenerateId(string productCode);

    public void Save()
    {
       var fooBarId = this.GenerateId("myproduct");
       //fooBarId will be used in this code block
    }
}
public class AnotherFooBar : FooBar
{
    public override int GenerateId(string productCode)
    {
       //..some logic to return an integer;
    }
}

您可以这样做:

    public abstract class FooBar
    {
        public void Save()
        {
            var fooBarId = this.GenerateId("myproduct");
            SaveCore(fooBarId);
            //fooBarId will be used in this code block
        }

        protected abstact void SaveCore(int id);
    }
现在强制子类在调用Save时调用该方法。我不知道SaveCore是否需要该id,如果需要,您可以像示例中一样将其作为参数传递

之后,如果不需要,您可以将GenerateId设置为私有,因为它的名称似乎不是您想要让人们自由做的事情


无论如何,要考虑好SaveCore和id所代表的内容,因为继承增加了实现的复杂性,而子类可能以错误的方式实现。

可以使基类抽象,并强制派生类实现方法。将必须调用GenerateId的方法放在基类中,并让它调用抽象方法:

public abstract class FooBar
{
    protected abstract string Product { get; }

    private int GenerateId(string productCode)
    {
       //..some logic to return an integer;
    }

    public void Save()
    {
       var fooBarId = this.GenerateId(Product);
       SaveInternal(fooBarId);
    }

    protected abstract void SaveInternal(int id);
}

public class AnotherFooBar : FooBar
{
    protected override string Product { get { return "myproduct"; } }

    protected override void SaveInternal(int id)
    {
       // id will be used in this code block
    }
}

此外,由于派生类可能希望为不同的产品生成ID,因此也在基类中创建一个抽象的只读产品属性,从而强制派生类提供产品名称。

有什么问题?您的代码工作正常。这取决于您希望何时调用该方法?继承基类的任何人都必须至少调用一次helper方法。是的,它可以正常工作,但它没有警告使用者需要首先调用helper方法。不,因为我不希望继承的类实现自己的GenerateId。基类必须这样做。啊,我明白了。。。那么法比奥和奥利弗的答案就可以满足你的要求了……谢谢。这就是我要找的!