惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security @ Cisco Blogs
H
Hacker News: Front Page
P
Privacy International News Feed
N
News and Events Feed by Topic
T
Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
K
Kaspersky official blog
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
L
Lohrmann on Cybersecurity
Jina AI
Jina AI
P
Proofpoint News Feed
AI
AI
雷峰网
雷峰网
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 叶小钗
Webroot Blog
Webroot Blog
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
罗磊的独立博客
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
月光博客
月光博客
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Securelist
W
WeLiveSecurity
T
Troy Hunt's Blog
A
Arctic Wolf
博客园 - 司徒正美

博客园 - 言午

Nopcommerce 二次开发2 Admin Nopcommerce 二次开发1 基础 Nopcommerce 二次开发2 WEB Nopcommerce 二次开发0 sqlce中不支持sp_rename修改表名 C#读取Excel遇到无法读取的解决方法 狼奔代码生成器 银行账户类 累 interface 抽象类 抽象方法 Message 类的继承 多态(虚方法) 委托 代理 索引! 第五周作业 第四周作业 考试! 线程安全 二 线程安全 一
事件event
言午 · 2012-04-11 · via 博客园 - 言午

委托,是事件基础

类的对象,发出消息,在运行时绑定处理方法。

以下,以animal为例,体温过高时,触发事件

1 先定一个个委托

delegate void MyDelegate();

2 在类中定义事件,并在某时刻触发。此例中在体温属性改变,大于37.5时触发。

    

class Animal
    {
        // 定义一个事件 体温过高  (先定义MyDelegate)
        public event MyDelegate highTemperature;
        float temperature;//体温

        public float Temperature
        {
            get { return temperature; }
            set
            {
                temperature = value; 
                //体温高时,触发事件。事件,一般在属性改变时触发
                if (temperature > 37.5)
                {
                    highTemperature();
                }
            }
        }      
    }

3 使用事件时,要将事件与处理事件的方法关联,然后改变属性触发事件。

定义一个方法,热了,就开空调

        

static void a_highTemperature()
        {
            Console.WriteLine("开空调");
        }

然后在main方法中写

            Animal a = new Animal();
            a.highTemperature += new MyDelegate(a_highTemperature); //事件 --- 处理方法
            a.Temperature = 37.6f;