更新时间:2023-02-26 19:24:01
您不能。您只能定义一个类时覆盖的方法。
You can't. You can only override a method when defining a class.
***的办法是转而使用适当的 Func键
委托作为一个占位符,并允许呼叫者提供实现这种方式:
The best option is instead to use an appropriate Func
delegate as a placeholder and allow the caller to supply the implementation that way:
public class SomeClass
{
public Func<string> Method { get; set; }
public void PrintSomething()
{
if(Method != null) Console.WriteLine(Method());
}
}
// Elsewhere in your application
var instance = new SomeClass();
instance.Method = () => "Hello World!";
instance.PrintSomething(); // Prints "Hello World!"