gpt4 book ai didi

oop - 我在哪里使用委托(delegate)?

转载 作者:行者123 更新时间:2023-12-03 04:44:30 25 4
gpt4 key购买 nike

现实世界中有哪些地方需要委托(delegate)?我很好奇在什么情况或模式下该方法是最佳解决方案。无需代码。

最佳答案

"Learning C# 3.0: Master the fundamentals of C# 3.0" 中所述

General Scenario: When a head of state dies, the President of the United States typically does not have time to attend the funeralpersonally. Instead, he dispatches a delegate. Often this delegate isthe Vice President, but sometimes the VP is unavailable and thePresident must send someone else, such as the Secretary of State oreven the First Lady. He does not want to “hardwire” his delegatedauthority to a single person; he might delegate this responsibility toanyone who is able to execute the correct international protocol.

The President defines in advance what responsibility will be delegated(attend the funeral), what parameters will be passed (condolences,kind words), and what value he hopes to get back (good will). He thenassigns a particular person to that delegated responsibility at“runtime” as the course of his presidency progresses.

In programming Scenario: You are often faced with situations where you need to execute a particular action, but you don’t know inadvance which method, or even which object, you’ll want to call uponto execute it.

For Example: A button might not know which object or objects need to be notified. Rather than wiring the button to a particularobject, you will connect the button to a delegate and then resolvethat delegate to a particular method when the program executes.

关于oop - 我在哪里使用委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com