代表是有用的,这就是为什么(示例程序) [英] Delegates are useful, and here is why (sample program)

查看:43
本文介绍了代表是有用的,这就是为什么(示例程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们通常在大多数教科书中都没有教过你我曾经看过

代表可以用来调用来自

''班级的班级方法不知道'委托',只要该类具有相同的签名

的方法(即如下,int Square(int))。


以下是显示该功能的示例。注意类UnAwareClass

的方法由类DelegateClass调用Square和Cuber。

这是因为UnAwareClass中的这些方法具有相同的签名

所以它们可以被DelegateClass调用,而没有关键字

''委托''出现在UnAwareClass中。


注意关键字'' static''必须如下使用,即使

UnAwareClass本身并不是静态的,尽管有一种方法可以使用具有非静态函数的
委托(但是我我觉得不需要这么做了。


如果你问我就很酷 - 就像C ++中的算子一样。


RL


//代表模型显示另一个班级(?UnAwareClass?)不是什么?b $ b甚至必须注意代表,仍然被叫和

就业。

///


///////

// OUTPUT(取数字的平方,此处为11,立方体为

给121和1331)


...现在为两个班级的代表外部使用...

Square 11是:121

!Cube 11是:1331

按任意键继续。 。 。

///////


使用System;

使用System.Collections.Generic;

使用System.Text;


命名空间EventDelegates

{

class program

{

static void Main(string [] args)

{


UnAwareClass myUnAwareClass = new UnAwareClass();


//现在可以从另一个班级访问代表


Console.WriteLine(" ...现在供外部使用代表来自两个
classes ...");


DelegateClass.PublicHigherPower2 sQr = new

DelegateClass.PublicHigherPower2(UnAwareClass.Squa re); //!注意:

如何调用:UnAwareClass.Square


DelegateClass myDelegateClass = new DelegateClass(); //

显然没有不良影响如果跟随而不是先前的




int ji2 = myDelegateClass.DoOp(sQr, 11);


Console.WriteLine(" Square 11 is:{0}",ji2);


DelegateClass.PublicHigherPower2 Cub2 = new

DelegateClass.PublicHigherPower2(UnAwareClass.Cube r);


// !!!注意:如何调用:UnAwareClass.Cuber


ji2 = myDelegateClass.DoOp(Cub2,11);


Console.WriteLine(&!立方体11是:{0},ji2);


// !!!注意意义:''委托''关键词绝对不会出现在课堂上

UnAwareClass(!)


}

}

}

////// //////

使用System;

使用System.Collections.Generic;

使用System.Text;


命名空间EventDelegates

{

class UnAwareClass

{


/ / !!在这个版本中,''delegate''关键字没有出现在

这个类(UnAwareClass)中,但只有DelegateClass类


int [] values;

int i;

public UnAwareClass()

{

values = new int [] {1,2,3 }; //未使用

i = 22333; //未使用

}

public static int Square(int x)

{

return x * x ;

}

public static int Cuber(int y)

{

return y * y * y;

}

}


class DelegateClass

{

public delegate int PublicHigherPower2(int x); //委托给

外部使用(当然必须在此声明关键字委托)


int j;

public DelegateClass ()

{

j = 0;

}


public int DoOp(PublicHigherPower2 ar, int x)//注释格式

{

返回ar(x);

}

}


}

They usually don''t teach you in most textbooks I''ve seen that
delegates can be used to call class methods from classes that are
''unaware'' of the delegate, so long as the class has the same signature
for the method (i.e., as below, int Square (int)).

Here is an example to show that feature. Note class "UnAwareClass"
has its methods Square and Cuber called by a class DelegateClass.
This is because these methods in UnAwareClass have the same signature
and so they can be called by DelegateClass, without the keyword
''delegate'' ever appearing in UnAwareClass.

Note the keyword ''static'' has to be used as below, even though
UnAwareClass itself is not static, though there is a way to use
delegates with non-static functions (however I don''t see the need to
do so).

Pretty cool if you ask me--like a functor in C++.

RL

//Delegate model showing how another class (?UnAwareClass?) does not
even have to be aware of the delegate and still be called and
employed.
///

///////
// OUTPUT (takes the square of a number, here 11, and the cube, to
give 121 and 1331)

...now for external use of delegates from two classes...
Square 11 is: 121
!Cube 11 is: 1331
Press any key to continue . . .
///////

using System;
using System.Collections.Generic;
using System.Text;

namespace EventDelegates
{
class Program
{
static void Main(string[] args)
{

UnAwareClass myUnAwareClass = new UnAwareClass();

// now to access delegate from another class

Console.WriteLine("...now for external use of delegates from two
classes...");

DelegateClass.PublicHigherPower2 sQr = new
DelegateClass.PublicHigherPower2(UnAwareClass.Squa re); //!!! Note: how
called: UnAwareClass.Square

DelegateClass myDelegateClass = new DelegateClass(); //
apparently no ill effects if follows rather than preceeds previous
line

int ji2 = myDelegateClass.DoOp(sQr, 11);

Console.WriteLine("Square 11 is: {0}", ji2);

DelegateClass.PublicHigherPower2 Cub2 = new
DelegateClass.PublicHigherPower2(UnAwareClass.Cube r);

//!!! note: how called: UnAwareClass.Cuber

ji2 = myDelegateClass.DoOp(Cub2, 11);

Console.WriteLine(" !Cube 11 is: {0}", ji2);

// !!!Note significance: ''delegate'' keyword NEVER APPEARS in class
UnAwareClass (!)

}
}
}
////////////
using System;
using System.Collections.Generic;
using System.Text;

namespace EventDelegates
{
class UnAwareClass
{

//!! in this version, ''delegate'' keyword does not appear in
this class (UnAwareClass) but only DelegateClass class

int[] values;
int i;
public UnAwareClass()
{
values = new int[] { 1, 2, 3 }; //not used
i = 22333; //not used
}
public static int Square(int x)
{
return x * x;
}
public static int Cuber(int y)
{
return y * y * y;
}
}

class DelegateClass
{
public delegate int PublicHigherPower2(int x); //delegate to
be used externally (keyword delegate must of course be declared here)

int j;
public DelegateClass()
{
j = 0;
}

public int DoOp(PublicHigherPower2 ar, int x) //note format
{
return ar(x);
}
}

}

推荐答案

有问题吗? (这就是这个小组的目标...)


但我不确定这是一个非常有用的例子

代表们...特别是,Main可以直接使用实际方法

,而不是使用委托。当调用它们的代码不可能知道实际的

方法(例如List< T> .Find)时,代表变得有用。我在数主要作为调用

代码,因为DelegateClass / DoOp实际上并没有添加任何东西 - 主要可以

就像轻松调用sQr(11)和Cub2(11)一样。 />

重新委托没有出现在UnAwareClass中,我不确定是否应该让我们感到惊讶?与C ++相比,目标方法也不知道关于void-pointer的
。而且还有很多未使用过的东西...

(i,j,值,2个构造函数) - 它只会增加混乱。


注意。 NET 3.5增加了一些可重用的泛型代理,例如

Func< int,可以用来代替声明你自己的代理。


只是一些指针。 .. [没有双关语]


Marc
Was there a question? (which is kinda what this group is aimed at...)

But I''m not sure that this is a massively useful example of
delegates... in particular, Main could simply use the actual method
directly, rather than using delegates. Delegates become useful when
the code that invokes them cannot possibly know about the actual
methods (such as List<T>.Find). I''m counting "Main" as the calling
code since DelegateClass/DoOp don''t actually add anything - Main could
just as easily call sQr(11) and Cub2(11).

Re delegate not appearing in UnAwareClass, I''m not sure that should
surprise us? By comparison to C++, the targeteted method wouldn''t know
about a void-pointer, either. And theer is a lot of unused stuff...
(i, j, values, 2 constructors) - it just adds confusion.

Note that .NET 3.5 adds some re-usable generic delegates such as
Func<int,intthat can be used in place of declaring your own.

Just some pointers... [no pun intended]

Marc


7月13日,12:56 * am,Marc Gravell< marc .grav ... @ gmail.comwrote:
On Jul 13, 12:56*am, Marc Gravell <marc.grav...@gmail.comwrote:

有问题吗? (这是该组的目标......)
Was there a question? (which is kinda what this group is aimed at...)



不,这是评论。

No, it was a comment.


>

但我不确定这是一个非常有用的例子

代表...特别是,Main可以简单地使用实际的方法

直接,而不是使用代表。
>
But I''m not sure that this is a massively useful example of
delegates... in particular, Main could simply use the actual method
directly, rather than using delegates.



请。你意识到代表甚至都不是必需的,而且你可以做一个委托可以做的所有事情(甚至是一个发布和订阅的事件代表)调用方法。更多

笨拙和代码维护的噩梦,但它的工作原理。代表们只是一个编程范例。

。有人说,委员会和事件是由Microsoft在C ++和C#的CLI语言中引入的,以使它们与Visual Basic更兼容(我读过一次)。 />

Please. You realize that delegates are not even necessary, ever, and
you can do everything a delegate can do (even an Event delegate that
publishes and is subscribed to) with simple calls to methods. More
clumsy and a code maintenance nightmare, but it works. Delegates are
just a programming paradigm. Some say delegates and events were
introduced in the CLI langauges of C++ and C# by Microsoft to make
them more compatible to Visual Basic (I read that once).


>

重新委托没有出现在UnAwareClass中,我不确定是否应该让我们感到惊讶?
>
Re delegate not appearing in UnAwareClass, I''m not sure that should
surprise us?



这让我感到惊讶,因为我的众多教科书中的所有例子都是反对的。

Well it surprised me, since all the examples in my numerous textbooks
were contra.


请注意,.NET 3.5添加了一些可重复使用的通用委托,例如

Func< int,可用于代替声明自己的委托。
Note that .NET 3.5 adds some re-usable generic delegates such as
Func<int,intthat can be used in place of declaring your own.



不知道这是如何相关的 - 通用模板是众所周知的,

另一个主题。


感谢您的回复。快乐的编码。


RL

Don''t see how this is relevant--generic templates are well known and
another subject.

Thanks for replying. Happy coding.

RL


7月13日,1:16 * pm,raylopez99< raylope ... @ yahoo .comwrote:
On Jul 13, 1:16*pm, raylopez99 <raylope...@yahoo.comwrote:

请。 *你意识到代表甚至都不是必需的,而且你可以做代理人可以做的所有事情(即使是一个发布和订阅的事件代表)对方法的简单调用。 *更多

笨拙和代码维护的噩梦,但它的工作原理。 *代表是

只是一个编程范例。 *有人说代表和事件是由Microsoft在C ++和C#的CLI语言中引入的,以使它们与Visual Basic更兼容(我读过一次)。
Please. *You realize that delegates are not even necessary, ever, and
you can do everything a delegate can do (even an Event delegate that
publishes and is subscribed to) with simple calls to methods. *More
clumsy and a code maintenance nightmare, but it works. *Delegates are
just a programming paradigm. *Some say delegates and events were
introduced in the CLI langauges of C++ and C# by Microsoft to make
them more compatible to Visual Basic (I read that once).



代理只是函数指针关闭了函数的第一个参数

。事件只是Observer

模式的语法糖。没有范例。对于他们来说,他们几乎没有什么可以用VB6做的b $ b:VB6有一些模糊的事件

语法,但不是代表。一个更好的例子是德尔福 -

它具有对象的功能。类型几乎相当于C#non

多播代表。

Delegates are just function pointers closed over the first argument of
the function. Events are just syntactic sugar for the Observer
pattern. There is no "paradigm" to them, and they have little to do
with VB6, either: VB6 had events which were vaguely similar
syntactically, but not delegates. A better example would be Delphi -
it had "function of object" type which was almost equivalent to C# non-
multicast delegates.


这篇关于代表是有用的,这就是为什么(示例程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆