日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多播委托(multicast delegate)

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多播委托(multicast delegate) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

還是老規矩,先看代碼:)

using?System;

class?MulticastTester
{
????
delegate?void?Greeting();
????
????
public?static?void?SayThankYou()
????
{
????????Console.WriteLine(
"Thank?you!");
????}


????
public?static?void?SayGoodMorning()
????
{
????????Console.WriteLine(
"Good?morning!");
????}


????
public?static?void?SayGoodnight()
????
{
????????Console.WriteLine(
"Goodnight");
????}

????
????
public?static?void?Main()
????
{
????????Greeting?myGreeting?
=?new?Greeting(SayThankYou);
????????Console.WriteLine(
"My?single?greeting:");
????????myGreeting();
????????
????????Greeting?yourGreeting?
=?new?Greeting(SayGoodMorning);????????
????????Console.WriteLine(
"\nYour?single?greeting:");
????????yourGreeting();
????????
????????Greeting?ourGreeting?
=?myGreeting?+?yourGreeting;
????????Console.WriteLine(
"\nOur?multicast?greeting:");
????????ourGreeting();

????????ourGreeting?
+=?new?Greeting(SayGoodnight);
????????Console.WriteLine(
"\nMulticast?greeting?which?includes?Goodnight:");
????????ourGreeting();
????????
????????ourGreeting?
=?ourGreeting?-?yourGreeting;
????????Console.WriteLine(
"\nMulticast?greeting?without?your?greeting:");
????????ourGreeting();
????????
????????ourGreeting?
-=?myGreeting;
????????Console.WriteLine(
"\nSingle?greeting?without?your?greeting?and?my?greeting:");
????????ourGreeting();
????}

}



在MulticastTester類的第一個語句就是我們的委托,跟上次的委托是一樣的定義方法,只是這次的委托Greeting沒有返回值[1]也沒有參數,其實想想也知道Greeting就只是一個動作而已:)

然后有三個靜態方法,說謝謝,說早安,說晚安。等待我們用委托來封裝。

在Main()方法中,我們首先用一個委托myGreeting封裝了SayThankYou,然后用另一個委托yourGreeting封裝了SayGoodMorning,這跟我們前面講的委托是一樣的。

第三個ourGreeting并沒有明顯地封裝一個方法,而是把前面兩個委托加了起來。這就是我們這次的主題——多播委托。這樣一加,ourGreeting就封裝了所加委托封裝的所有方法。

第四個委托還是我們的ourGreeting,但是它又封裝了一個新的方法SayGoodNight,并使用了操作符+=,這時候我們的ourGreeting封裝了三個方法。

那我突然后悔了,要除去一個方法怎么辦?那么就請看我們第五個和第六個委托(其實都是ourGreeting,呵呵),使用兩種辦法除去方法。

經過我的測試,ourGreeting = ourGreeting - new Greeting(SayThankYou)這樣也是可以多播委托中的方法的。

[1]多播委托必須返回void

轉載于:https://www.cnblogs.com/wdxinren/archive/2004/11/23/67474.html

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的多播委托(multicast delegate)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。