帮助理解代表? [英] Help understanding delegates?

查看:72
本文介绍了帮助理解代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地了解以下代码的工作原理。我已经在下面发布了

的问题。


名称空间Something.Something1

{

使用系统;


public delegate void Test1();

public delegate void Test2(ink k);


公共类A

{

public static void Log1(int l)

{

Console.WriteLine(" ;:{0}",l);

}


public void Log2(int l)

{

Console.WriteLine(&#; 0}",l);

}

}


公共类Class1

{

public static void Main()

{

A a = new A ();

Test2 t0 = new Test2(A.Log1); //使用静态方法

Test2 t1 = new Test2(a.Log2); //使用实例方法


t0 + = t1; t0(0);

t0 - = t1; t0(1);

}

}

}


这将打印: 0#0:1。当没有传递param时,Log1如何打印0

到它和l未初始化为0?


通过

代表的静态和实例方法调用有什么区别?


这一行t0 - = t1; T0(1);从委托中删除那些方法

集合是否正确?如果该行不存在
,输出会如何影响?


另外,如果我使用IE对象并且连续三次加载页​​面,将每个页面加载后,以下每个事件分别触发
,而不是每个页面加载后总是触发的所有三个?换句话说,在第一页加载完成后,

将触发onIEocComplete1。第二次

页面加载完成后,onIEocComplete2将触发。在第三页加载

完成后,onIEocComplete3将触发。


IE_Inst.DocumentComplete + = new

SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this .onIEDocComplete1);


IE_Inst.DocumentComplete + = new

SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete2);


IE_Inst.DocumentComplete + = new

SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete3);


谢谢,

Brett

I''d like to better understand how the following code works. I''ve posted
questions below.

namespace Something.Something1
{
using System;

public delegate void Test1();
public delegate void Test2(ink k);

public class A
{
public static void Log1 (int l)
{
Console.WriteLine(":{0}",l);
}

public void Log2 (int l)
{
Console.WriteLine("#{0}",l);
}
}

public class Class1
{
public static void Main()
{
A a = new A();
Test2 t0 = new Test2(A.Log1); //use static method
Test2 t1 = new Test2(a.Log2); //use instance method

t0 += t1; t0(0);
t0 -= t1; t0(1);
}
}
}

This will print ":0 #0 :1". How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?

What are the differences in the static and instance method calls via
delegates?

This line t0 -= t1; t0(1); removes those methods from the delegate
collection correct? How is the output affected if that line does not
exists?

Also, if I''m using the IE object and have three successive page loads, will
each of the following events respectively fire after each page load rather
than all three always firing after each page load? In other words, after
the first page load completes, onIEDocComplete1 fires. After the second
page load completes, onIEDocComplete2 fires. After the third page load
completes, onIEDocComplete3 fires.

IE_Inst.DocumentComplete += new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete1);

IE_Inst.DocumentComplete += new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete2);

IE_Inst.DocumentComplete += new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete3);

Thanks,
Brett

推荐答案

当没有param传递给它时,Log1如何打印0和l未初始化为0?


你是什么意思没有传递给它的参数?

调用委托时你传递的是0,这就是l将得到的值。


静态和实例有什么区别方法通过
代表打电话?


除了在常规方法调用中看到的区别之外没有区别。


此行t0 - = t1; T0(1);从委托
集合中删除那些方法是否正确?


从t0中删除t1方法(Log2),但保留Log1。


如果该行不存在,输出会受到影响?
How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?
What do you mean no param is passed to it? You''re passing 0 when
invoking the delegate so that''s the value l will get.

What are the differences in the static and instance method calls via
delegates?
No differences apart from the ones seen in regular method calls.

This line t0 -= t1; t0(1); removes those methods from the delegate
collection correct?
It removes the t1 methods (Log2) from t0, but leaves Log1.

How is the output affected if that line does not exists?




为什么不试试看看。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



Why don''t you try it and see.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.




" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道

news:uf ************** @ TK2MSFTNGP14.phx.gbl ...

"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:uf**************@TK2MSFTNGP14.phx.gbl...
当没有param传递给它时,Log1如何打印0和l。是不是初始化为0?
How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?



你是什么意思没有传递给它的参数?当
调用委托时你正在传递0,这样'我将得到的值。



What do you mean no param is passed to it? You''re passing 0 when
invoking the delegate so that''s the value l will get.




我的意思是说t1或Log2。零没有通过它它显示。


关于IE问题的任何建议?



I mean to say t1 or Log2. Zero is not passed it yet it is displayed.

Any suggestions on the IE question?


>>>当没有传递param时,Log1如何打印0
>>>How does Log1 print 0 when no param is passed
to it andl是不是初始化为0?
你是什么意思没有传递给它的param?当
调用委托时你正在传递0,这样'我将得到的值。
to it and "l" is not initialized to 0?
What do you mean no param is passed to it? You''re passing 0 when
invoking the delegate so that''s the value l will get.



我的意思是说t1或Log2。零没有被传递但它被显示。



I mean to say t1 or Log2. Zero is not passed it yet it is displayed.




当你调用t0委托

时,用参数0调用Log2,因为Log2是在你执行t0 + = t1之后调用t0的方法列表。


关于IE问题的任何建议?



Log2 is called with the argument 0 when you invoke the t0 delegate
because Log2 is in t0''s list of methods to call after you do t0 += t1.

Any suggestions on the IE question?


另外,如果我正在使用IE对象并且有三个连续的页面加载,则每个页面加载后将分别触发以下每个事件
每个页面加载后总是触发所有三个?
Also, if I''m using the IE object and have three successive page loads, will
each of the following events respectively fire after each page load rather
than all three always firing after each page load?




不会为每个加载事件触发它们。如果那不是你想要的b $ b,你将不得不删除onIEDocComplete1处理程序并在调用onIEocComplete1时添加

onIEocComplete2。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



No they will all fire for every load event. If that''s not what you
want, you''ll have to remove the onIEDocComplete1 handler and add the
onIEDocComplete2 one when onIEDocComplete1 is called.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


这篇关于帮助理解代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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