c# - 如何覆盖方法保存模板方法模式
问题描述
我确实有一个抽象类,在构造函数上我正在调用在同一个类上声明的抽象方法,我这样做是因为我确实想强制所有派生类不仅实现我的抽象方法而且调用它(我认为这就是模板方法模式的用武之地)。
顺便说一句:我知道这不是确保所有内容都正确编写的最佳方法,但由于外部问题,我想尽量减少我未来因实施不当而出现的问题。片段如下:
public abstract class Generic
{
public Generic()
{
Console.WriteLine("This is the generic");
this.SetRules();
}
protected abstract void SetRules();
}
好吧,在我的一个派生类上需要在声明上设置一个私有变量,我想在重写方法中使用这个变量。由于我不知道我的每个派生类需要多少种可能性,我想根据需要在我的每个派生类中声明它。下面的一个例子:
public class Derivated2 : Generic
{
private int _parameter;
public Derivated2(int parameter)
{
this._parameter = parameter;
}
protected override void SetRules()
{
int x = 0;
x = 1 + 3;
Console.WriteLine(String.Concat("This is the derivated2 with x: ", x, "; _parameter: ", _parameter));
}
}
现在来解决问题;
当我调用我的 derivated2 类时,我无法使用/查看声明中指定的变量值:
public static void Main()
{
Derivated2 _class1 = new Derivated2(2);
}
我的输出是:
This is the generic
This is the derivated2 one with x: 4; _parameter: 0
如何在覆盖的 asbtract 方法上查看/使用我的变量?如果我不能,那么无论开发人员做什么并且还可以在他的派生类中使用私有变量,实现我的目标的最佳方法是什么,确保 SetRules 方法将被调用?
这是我完成的完整代码:
解决方案
问题是您正在调用 Generic 构造函数,它将在调用 Derivated2 构造函数之前调用 SetRules,这意味着您的参数尚未设置。你可以试试这个。
public abstract class Generic
{
public Generic(int parameter)
{
Console.WriteLine("This is the generic");
this.SetRules(parameter);
}
protected abstract void SetRules(int paramter);
}
public class Derivated2 : Generic
{
public Derivated2(int parameter) : base(parameter){ }
protected override void SetRules(int parameter)
{
int x = 0;
x = 1 + 3;
Console.WriteLine(String.Concat("This is the derivated2 with x: ", x, "; _parameter: ", parameter));
}
}
public static void Main()
{
Derivated2 _class1 = new Derivated2(2);
}
正如您所说,每次要创建新类时都必须调用 SetRules 并不好,所以这意味着您不必(快速将其放在一起,因此您需要修复它)
public abstract class Generic
{
public Generic()
{
Console.WriteLine("This is the generic");
}
public abstract void SetRules();
}
public sealed class Derivated2 : Generic
{
int _param;
public Derivated2(RulesWithParameters rules) {
_param = rules.Parameter;
}
public override void SetRules()
{
int x = 0;
x = 1 + 3;
Console.WriteLine(String.Concat("This is the derivated2 with x: ", x, "; _parameter: ", _param));
}
}
public void Main()
{
var rules = new RulesWithParameters{
Parameter = 5
};
var _class1 = FactoryMethod<Derivated2>(rules);
var _class2 = FactoryMethod<Derivated1>(null);
}
public class Derivated1 : Generic
{
public Derivated1(Rules rules)
{
}
public override void SetRules()
{
Console.WriteLine("This is the derivated1");
}
}
public class Rules
{
public string RuleName {get; set;}
}
public class RulesWithParameters : Rules{
public int Parameter { get; set;}
}
public Generic FactoryMethod<T>(Rules rules) where T : Generic
{
T instance = (T)Activator.CreateInstance(typeof(T), rules);;
instance.SetRules();
return instance;
}
推荐阅读
- jquery - 我无法执行搜索
- javascript - 如何在谷歌饼图中显示前 5 个值
- docusignapi - DocuSign Connect - 带有 REST API v2.1 和 Java 库的 HMAC 安全性
- accessibility - NVDA浏览模式焦点是否应该让导航焦点移动?
- python - 无法从 cmd 提示符启动 python Jupyter Notebook
- sql-server - 降低逻辑读取
- mysql - MySQL Order BY,其中值以特定顺序由“:”分隔存储
- r - r plotly 不产生表面图
- c# - 如何检查字符串是否在特定位置包含字符?
- android - 将 android 应用程序迁移到 Firebase Crashlytics 的问题