标题:请教一下event的用法
只看楼主
skyzoneskyzone
Rank: 1
等 级:新手上路
威 望:1
帖 子:137
专家分:0
注 册:2008-10-6
 问题点数:0 回复次数:2 
请教一下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

说白了就是其中的
if (Event != null)
            {
                Event(this, new EventArgs());   //这一句不理解是什么意思。
             }

[[it] 本帖最后由 skyzoneskyzone 于 2008-12-4 09:29 编辑 [/it]]
搜索更多相关主题的帖子: Event eventhandler 抽象 
2008-12-04 09:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-247660-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.867317 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved