C#中:什么是虚拟事件,如何对它们使用
1个回答
2017-02-15 · 知道合伙人软件行家
关注
展开全部
事件可以被标记为通过使用虚拟关键字的虚拟事件。这使派生类通过使用override关键字重写事件行为。重写虚拟事件的事件也可以被密封,它指定为派生类不再虚拟
using System;
class Base
{
public virtual event EventHandler Foo
{
add
{
Console.WriteLine("Base Foo.add called");
}
remove
{
Console.WriteLine("Base Foo.remove called");
}
}
}
class Derived : Base
{
public override event EventHandler Foo
{
add
{
Console.WriteLine("Derived Foo.add called");
}
remove
{
Console.WriteLine("Derived Foo.remove called");
}
}
}
class Test
{
static void Main()
{
Base x = new Derived();
x.Foo += (sender, args) => {};
}
}
using System;
class Base
{
public virtual event EventHandler Foo
{
add
{
Console.WriteLine("Base Foo.add called");
}
remove
{
Console.WriteLine("Base Foo.remove called");
}
}
}
class Derived : Base
{
public override event EventHandler Foo
{
add
{
Console.WriteLine("Derived Foo.add called");
}
remove
{
Console.WriteLine("Derived Foo.remove called");
}
}
}
class Test
{
static void Main()
{
Base x = new Derived();
x.Foo += (sender, args) => {};
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询