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

推荐订阅源

WordPress大学
WordPress大学
博客园 - 司徒正美
Last Week in AI
Last Week in AI
博客园 - 聂微东
Jina AI
Jina AI
月光博客
月光博客
爱范儿
爱范儿
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
T
Tailwind CSS Blog
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
罗磊的独立博客
小众软件
小众软件
雷峰网
雷峰网
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog

博客园 - laifangsong

请问福州哪里卖打折计算机图书,又可按原价开发票的? 关于目录访问的iis配置问题 福州招聘asp程序员 qq聊天的小秘密 吃鱼引发的问题和中国式管理 为什么点击flash链接到本页面,Request.Referrer将无法获得url来源 观察。总结 - 思想(一) 在csdn上看到奶牛问题,写了下算法 C#(1.1)邮件发送类,功能全面,调用灵活、方便 思考_070614 asp中JMail(4.4)发送邮件 单个文件上传类(可以自定义配置) iis6(win2003)中的aspnet_client和iis5,5.1(win2000,winxp)不一样,系统迁移时一定要注意。 看似诡异的session赋值错误 不错的分页存储过程(转) 矩阵相乘(c) 取奇数游戏(c) 用递归算法求和为指定值N的所有组合 汉若塔问题(c)
委托:两个例子(主人仆人、打游戏)
laifangsong · 2007-01-25 · via 博客园 - laifangsong

using System;

namespace gainian.DelegateExample
{
    
    
public delegate void EatEventHander(object sender, EatEventArgs e);
    
    
public class EatEventArgs: EventArgs
    
{
        
public string EatWhere;
        
public decimal EatMoney;
        
        
public EatEventArgs(string EatWhere, decimal EatMoney)
        
{
            
this.EatWhere = EatWhere;
            
this.EatMoney = EatMoney;
        }

    }

    
    
public class Delegate1
    
{
//        static void Main(string[] args)
//        {
//            Master master = new Master();
//            Servant servant = new Servant();
//            
//            master.EatEvent += new EatEventHander(servant.Arrange);
//            master.OnHungry("福州大酒店", 1000);
//            
//            Console.ReadLine();    
//        }
    }


    
class Master
    
{                
        
public event EatEventHander EatEvent;        
        
        
private void OnEatEvent(EatEventArgs e)
        
{
            
if(EatEvent!=null)
            
{
                EatEvent(
this, e);
            }

        }

        
        
public void OnHungry(string EatWhere, decimal EatMoney)
        
{
            EatEventArgs e 
= new EatEventArgs(EatWhere, EatMoney);
            
            Console.WriteLine(
"主人:我肚子有点饿了!");
            Console.WriteLine(
"主人:我准备去 {0}吃饭,",EatWhere);
            Console.WriteLine(
"主人:准备消费 {0} !",EatMoney);
            Console.WriteLine(
"主人:仆人,快替我准备预定!\n");
            
            OnEatEvent(e);
        }

        
    }

    
    
class Servant
    
{
        
public void Arrange(object sender, EatEventArgs e)
        
{
            Console.WriteLine(
"仆人:我已接到您的指示了!");
            Console.WriteLine(
"仆人:您准备去 {0}吃饭,",e.EatWhere);
            Console.WriteLine(
"仆人:您消费 {0} !",e.EatMoney);
            Console.WriteLine(
"仆人:我马上帮您预定!\n");            
        }

    }
    

}

using System;

namespace gainian.DelegateExample
{
        
    
public delegate void PlaygameEventHandle(Employee emp);
    
    
/// <summary>
    
/// Delegate2 的摘要说明。
    
/// </summary>

    public class Delegate2
    
{
        
public Delegate2()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
        
static void Main(string[] args)
        
{
            XiaoLi xiaoli 
= new XiaoLi("小李",1000);
            XiaoZhang xiaozhang 
= new XiaoZhang("小张",1000);
            
            Console.WriteLine(
"小李原来的薪水是 {0}", xiaoli.Salary);
            
            xiaoli.playgameEvent 
+= new PlaygameEventHandle(xiaozhang.SubtractSalary);
            xiaoli.OnPlaygame();
            
            Console.WriteLine(
"小李打游戏后的薪水是 {0}", xiaoli.Salary);
            
            Console.ReadLine();
        }

        
    }

    
    
public class Employee
    
{
        
private string name;
        
        
private decimal salary;
        
        
public Employee(string name, decimal salary)
        
{
            
this.name = name;
            
this.salary = salary;
        }

        
        
public string Name
        
{
            
get
            
{
                
return this.name;
            }

            
set
            
{
                
this.name = value;
            }

        }

        
        
public decimal Salary
        
{
            
get
            
{
                
return this.salary;
            }

            
set
            
{
                
this.salary = value;
            }

        }

        
        
public virtual void OnPlaygame()
        
{
            
this.salary -= 10;
        }

                
    }

    
    
public class XiaoLi: Employee
    
{
        
public XiaoLi(string name, decimal salary) : base(name,salary)
        
{}
        
        
public event PlaygameEventHandle playgameEvent;
        
        
public override void OnPlaygame()
        
{
            Console.WriteLine(
"\n小李正在打游戏。。。\n");
            
if(playgameEvent!=null)
            
{
                playgameEvent(
this);
            }

        }

        
    }
    
    
    
public class XiaoZhang: Employee
    
{
        
public XiaoZhang(string name, decimal salary) : base(name,salary)
        
{}
        
        
public void SubtractSalary(Employee emp)
        
{
            Console.WriteLine(
this.Name + " 发现 " + emp.Name + " 在打游戏。");
            emp.Salary 
-= 100;
            Console.WriteLine(
this.Name + " 扣了 " + emp.Name + " 100块钱!\n");
        }

    }

    
    
}