【发布时间】:2015-04-02 11:14:44
【问题描述】:
我在 C# 中创建了一个使用“Action”方法的类。
public void Action()
{
}
该方法是空的,因为当创建该类的新实例时,用户应该能够定义该方法的作用。一个用户可能需要该方法来写入控制台,另一个用户可能希望它为变量赋值等。我有什么办法可以改变该方法在其原始定义之外可以做什么,沿着以下:
//Using the instance "MyClass1", I have assigned a new action to it (Writing to the console)
//Now the method will write to the console when it is called
MyClass1.Action() = (Console.WriteLine("Action"));
【问题讨论】:
-
您可能想研究委托和匿名方法。但这似乎是一种相当不稳定的代码执行方式。你考虑过继承吗?
标签: c# .net class methods instance