C 事件與委託簡單實現

2021-06-18 16:47:19 字數 1174 閱讀 7795

假設我們有個高檔的熱水器(heater),我們給它通上電,當水溫超過95度的時候:1、揚聲器(alarm)會開始發出語音,告訴你水的溫度;2、液晶屏(display)也會改變水溫的顯示,來提示水已經快燒開了。

可以建立如下事件與委託(在控制台下實現):

heater.cs

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

}protected virtual void onboiled(boiledeventargs e)

public void boilwater()}}

}}

}

display.cs

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

:警告:水已經度了!!", heater.bread, e.temperature.tostring());}}

}

alarm.cs

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

:水已經度了!", heater.bread, e.temperature.tostring());}}

}

program.cs

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

}}

C 委託與事件區別簡單總結

前天接了個 面試,被問到事件與委託的區別,雖然一直用但真要你說有什麼區別一時半會還真說不上來。於是問google老師,得到如下答案 2.委託可以在外部被其他物件呼叫,而且可以有返回值 返回最後乙個註冊方法的返回值 而事件不可以在外部呼叫,只能在宣告事件的類內部被呼叫。我們可以使用這個特性來實現觀察者...

C 委託與事件 簡單筆記

簡單記錄點東西 適合似懂非懂的朋友看看 委託型別用來定義和響應應用程式中的 藉此可以設計各種有物件導向特性的 模式。下面要說的事件在我看來就是委託的一種實現,再深一步講,利用委託加事件,是不是可以構建各種所謂的管道框架。如asp.net webapi的管道模型,整個訊息處理管道是通過一組有序的htt...

c 委託與事件

c 委託與事件 心得 c 用委託來實現事件通知機制。委託相當與c 函式指標。整個過程涉及乙個呼叫者,乙個被呼叫者,還有就是這個委託。實現步驟有以下幾步 1.申明委託,2.定義呼叫者和呼叫的函式,3.定義被呼叫者和具體實現的函式 被呼叫的函式 1.申明委託 在包裡或者類裡,public public ...