这是一个示例代码,当使用虚拟方法和覆盖时,它不会像我预期的那样工作。为什么?
main()
{
Child c = new Child();
c.Trigger = new Trigger();
}
public class Parent
{
public Trigger _trigger;
public Trigger Trigger
{
set
{
_trigger = value;
_trigger.TriggerEvent += Trigger.TriggerHandler(OnTrigger);
}
}
internal virtual void OnTrigger(int type)
{
Write("I don't want to write this!");
}
}
public class Child : Parent
{
internal override void OnTrigger(int type)
{
Write("I want this to be written, but it's not.");
}
}
订阅父事件的方法“OnTrigger”不会被覆盖。如何使父级中订阅的事件执行子级的方法?