您的位置:首页 > 编程语言 > C#

C#重点知识详解(五 代理:一)

2001-04-26 15:10 1386 查看
导 读:代理实现的是象c++等语言的指针功能,不同于函数指针,代理是一种面向对象、安全类型的。

作者: wkrain   www.ASPCool.com 

第五章:代理 

代理实现的是象c++等语言的指针功能,不同于函数指针,代理是一种面向对象、安全类型的。代理事派生于公共基类(system)的一种参考类型,方法被压入一个代理中,对于实例方法被称为实例的组成实体或关于实例的方法,而静态方法,被称为类的组成实体或类方法。代理的强大功能是它可以自动的匹配方法,而不管其类型。 

写一个代理包括三个步骤: 

写代理、实例化、调用。 

代理的声明使用以下语法: 
delegate void SimpleDelegate(); 

实例化一个代理 
class Test 

static void F() { 
System.Console.WriteLine("hello world"); 

static void Main() { 
SimpleDelegate d = new SimpleDelegate(F);//将方法压入 
d();//通过代理; 
F();//不通过代理; 



最后让我们调用她 

void MultiCall(SimpleDelegate d, int count) { 
for (int i = 0; i < count; i++) 
d(); 



我们可以看到对于方法的调用是通过代理来完成的,调用时并不需要知道被调用她的类型。代理在我看来好比是对象要一件事她不直接地调用这个方法,而是通过一个中间人去调用她。 

下面就代理的强大功能进行详细介绍:首先然我们实现一个这样的功能,考虑一下该如何用指向基类的对象调用子类的成员函数。在这里程序员是不是点怀恋指针了,不过在c#中这样的功能完全也可实现的,使用一个单独的代理我们可以完成这项功能。以下代码来自Timothy A. Vanover文章。 

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)); 



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# c++ 语言 system