简体   繁体   English

如何使用方法实现基类并强制派生类重写它?

[英]How to Implement a Base Class with a Method and yet force the Derived Class to Override it?

Having something like this this : 有这样的事情:

public abstract class AAA
{
  protected abstract virtual string ToString()    // Error
  {
    // Base Stuff
  }
}

public abstract class BBB : AAA
{
  public override string ToString()
  {
    // Use base.ToString();
    // More Stuff
  }
}

I read another post ( abstract method in a virtual class ) witch was pretty like my question, but there is a little difference. 我读了另一篇文章( 虚拟类中的抽象方法 ),巫婆很像我的问题,但有一点区别。 I'd like AAA.ToString() to have a base behavior and force the derived class to override it. 我希望AAA.ToString()具有基本行为, 强制派生类重写它。

I know I could do something like this (scroll down) but I was looking for a proper way. 我知道我可以做这样的事情(向下滚动),但我一直在寻找正确的方法。

public abstract class AAA
{
  public abstract string ToString();
  protected string ToString2()
  {
    // Base Stuff
  }
}

public class BBB : AAA
{
  public override string ToString()
  {
    // Use base.ToString2();
    // More Stuff
  }
}

I could also probably make a IAAA interface and build my BBB class like 我还可以制作一个IAAA接口并像这样建立我的BBB类

public class BBB : AAA, IAAA { ... }

but don't look right to me. 但别看我正确。

Your approach is essentially the proper way... 你的方法基本上正确的方式...

Edit: 编辑:

Your "specification" is: 您的“规格”是:

  1. Default behaviour 默认行为
  2. Required override 必需的替代

Now, since you appear to want the "default behaviour" to always be executed, you need 现在,由于您似乎希望始终执行“默认行为”,因此您需要

public string ToString()
{
    // placeholder for some default behaviour

    string result = doToString();

    // placeholder for some more default behaviour

    return result;
}

protected abstract string doToString();

This only works if you know what the base class is doing in general, and want to provide the implementation for doToString() in the derived class. 仅当您知道基类通常在做什么并且要在派生类中提供doToString()的实现时, doToString() This is known as the Template Method design pattern. 这称为模板方法设计模式。

However, if you are looking for a way to require the implementer of a derived class to call base.ToString() in its implementation of ToString() , then there's no way to do this. 但是,如果您正在寻找一种方法来要求派生类的实现者在其ToString()实现中调用base.ToString() ToString() ,则无法做到这一点。 By allowing an override, you give the derived class control over its implementation. 通过允许覆盖,可以使派生类对其实现进行控制。

You can document your recommendation to always call base.ToString() , but that's about it. 您可以记录您的建议以始终调用base.ToString() ,仅此而已。

Using an interface defines the public contract for your class. 使用接口可以为您的班级定义公共合同。 This doesn't give you any additional benefit over the Template Method pattern, since you still need to derive from a base class to obtain the "default" implementation. 与模板方法模式相比,这没有给您带来任何其他好处,因为您仍然需要从基类派生以获得“默认”实现。

Do you have any details about what you are trying to provide in your base class as the "default behaviour"? 您是否有关于要在基类中提供的“默认行为”的详细信息? Maybe that will help come to a better solution. 也许这将有助于寻求更好的解决方案。 Looking at the referenced question, this is essentially the same approach. 看所参考的问题,这基本上是相同的方法。

I just found out what to do to get exactly the behavior I wanted : 我只是发现该怎么做才能确切地得到我想要的行为:

public abstract class AAA
{
  protected abstract string ToStringSpecific();
  protected string ToString()
  {
    // Base Stuff
    ...
    // Use this.ToStringSpecific();
  }
}

public class BBB : AAA
{
  protected override string ToStringSpecific()
  {
    // Specific Stuff
  }
}

No, it isn't possible to turn a currently virtual function into an abstract function. 不,不可能将当前的虚拟函数转换为抽象函数。 The only way of enforcing this would be to create another function that's abstract, as you suggested. 强制执行此操作的唯一方法是按照您的建议创建另一个抽象的函数。 This is arguably a more appropriate approach (and I would agree with that). 可以说这是一种更合适的方法(我同意)。

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

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