在 C# 中为模板/抽象类的构造函数实现策略的最佳方法是什么?我有几个类都基于解析构造函数中的字符串。解析是在一个静态方法中完成的,该方法创建键值对列表并且对于所有类都是通用的,但是某些字段对于所有类也是通用的——因此我使用了一个抽象模板类。
问题是我没有看到继承抽象基类的构造函数实现的方法。否则,我将在基类中实现构造函数策略,并强制在一些抽象方法中处理列表。
编辑:为模板类添加了无效代码
public abstract class XXXMessageTemplate
{
public XXXMessageTemplate(string x) // implementation for the constructor
{
Parse(x);//general parse function
CommonFields();//filling common properties
HandlePrivateProperties();//fill individual properties
HandlePrivateStructures();//fill individual structures
}
abstract void HandlePrivateProperties();
abstract void HandlePrivateStructures();
}
The actual messages should not implement any constructor and only implement the HandlePrivateProperties and HandlePrivateStructures functions.