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

估计只有会的人能看懂

2005-05-15 00:13
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
我 不会。。。

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-05-16 10:10
快速回复:[转帖]C#代理(Delegate)
数据加载中...
 
   



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

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