| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 747 人关注过本帖
标题:[转帖]C#代理(Delegate)
取消只看楼主 加入收藏
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
[转帖]C#代理(Delegate)

namespace DelegatesCS { using System;

public class Wisdom //包含代理的类 { public delegate string GiveAdvice(); public string OfferAdvice(GiveAdvice Words) { return Words(); } } public class Parent //基类 { public virtual string Advice() { return("Listen to reason"); } ~Parent() {} } public class Dad: Parent //子类 { public Dad() {} public override string Advice() { return("Listen to your Mom"); } ~Dad() {} } public class Mom: Parent //子类 { public Mom() {} public override string Advice() { return("Listen to your Dad"); } ~Mom() {} } public class Daughter //不继承与基类的类 { public Daughter() {} public string Advice() { return("I know all there is to life"); } ~Daughter() {} }

public class Test { public static string CallAdvice(Parent p)//使用基类 { Wisdom parents = new Wisdom(); Wisdom.GiveAdvice TeenageGirls = new Wisdom.GiveAdvice(p.Advice);//将Advice方法委托给TeenageGirls委托对象 return(parents.OfferAdvice(TeenageGirls)); }

public static void Main() { Dad d = new Dad(); Mom m = new Mom(); Daughter g = new Daughter(); //以下两个为衍于基类的类 Console.WriteLine(CallAdvice(d)); Console.WriteLine(CallAdvice(m)); //以下为未衍于基类的类,如果调用将出错。 //Console.WriteLine(CallAdvice(g)); } } }

搜索更多相关主题的帖子: Delegate 转帖 代理 
2005-05-15 00:11
快速回复:[转帖]C#代理(Delegate)
数据加载中...
 
   



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

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