请教一下event的用法
using System;
using System.Collections.Generic;
using System.Text;
namespace Example04
{
#region 基类,抽象类
public abstract class BaseClass
{
//抽象属性,同时具有get和set访问器表示继承类必须将该属性实现为可读写
public abstract String Attribute
{
get;
set;
}
//抽象方法,传入一个字符串参数无返回值
public abstract void Function(String value);
//抽象事件,类型为系统预定义的代理(delegate):EventHandler
public abstract event EventHandler Event;
//抽象索引指示器,只具有get访问器表示继承类必须将该索引指示器实现为只读
public abstract Char this[int Index]
{
get;
}
}
#endregion
#region 继承类
public class DeriveClass : BaseClass
{
private String attribute;
public override String Attribute
{
get
{
return attribute;
}
set
{
attribute = value;
}
}
public override void Function(String value)
{
attribute = value;
if (Event != null)
{
Event(this, new EventArgs()); //这一句不理解是什么意思。
}
}
public override event EventHandler Event;
public override Char this[int Index]
{
get
{
return attribute[Index];
}
}
}
#endregion
class Program
{
static void OnFunction(object sender, EventArgs e)
{
for (int i = 0; i < ((DeriveClass)sender).Attribute.Length; i++)
{
Console.WriteLine(((DeriveClass)sender)[i]);
}
}
static void Main(string[] args)
{
DeriveClass tmpObj = new DeriveClass();
tmpObj.Attribute = "1234567";
Console.WriteLine(tmpObj.Attribute);
//将静态函数OnFunction与tmpObj对象的Event事件进行关联
tmpObj.Event += new EventHandler(OnFunction);
tmpObj.Function("7654321");
Console.ReadLine();
}
}
}
结果:
1234567
7
6
5
4
3
2
1
资料引用:file:///C:/Documents%20and%20Settings/Administrator/桌面/C%23基础概念二十五问_C%23教程_www_knowsky_com.htm
using System.Collections.Generic;
using System.Text;
namespace Example04
{
#region 基类,抽象类
public abstract class BaseClass
{
//抽象属性,同时具有get和set访问器表示继承类必须将该属性实现为可读写
public abstract String Attribute
{
get;
set;
}
//抽象方法,传入一个字符串参数无返回值
public abstract void Function(String value);
//抽象事件,类型为系统预定义的代理(delegate):EventHandler
public abstract event EventHandler Event;
//抽象索引指示器,只具有get访问器表示继承类必须将该索引指示器实现为只读
public abstract Char this[int Index]
{
get;
}
}
#endregion
#region 继承类
public class DeriveClass : BaseClass
{
private String attribute;
public override String Attribute
{
get
{
return attribute;
}
set
{
attribute = value;
}
}
public override void Function(String value)
{
attribute = value;
if (Event != null)
{
Event(this, new EventArgs()); //这一句不理解是什么意思。
}
}
public override event EventHandler Event;
public override Char this[int Index]
{
get
{
return attribute[Index];
}
}
}
#endregion
class Program
{
static void OnFunction(object sender, EventArgs e)
{
for (int i = 0; i < ((DeriveClass)sender).Attribute.Length; i++)
{
Console.WriteLine(((DeriveClass)sender)[i]);
}
}
static void Main(string[] args)
{
DeriveClass tmpObj = new DeriveClass();
tmpObj.Attribute = "1234567";
Console.WriteLine(tmpObj.Attribute);
//将静态函数OnFunction与tmpObj对象的Event事件进行关联
tmpObj.Event += new EventHandler(OnFunction);
tmpObj.Function("7654321");
Console.ReadLine();
}
}
}
结果:
1234567
7
6
5
4
3
2
1
资料引用:file:///C:/Documents%20and%20Settings/Administrator/桌面/C%23基础概念二十五问_C%23教程_www_knowsky_com.htm
说白了就是其中的
if (Event != null)
{
Event(this, new EventArgs()); //这一句不理解是什么意思。
}
[[it] 本帖最后由 skyzoneskyzone 于 2008-12-4 09:29 编辑 [/it]]