委託的高階使用

2022-04-08 21:11:39 字數 2545 閱讀 7024

多播委託(multicast)乙個委託裡面包含了不止乙個方法

static

void main(string

args)

;student stu2 = new student ;

student stu3 = new student ;

action action1 = new

action(stu1.dohomework);

action action2 = new

action(stu2.dohomework);

action action3 = new

action(stu3.dohomework);

action1 +=action2;

action1 +=action3;

action1();

//多播委託,根據封裝方法的順序

//action1();

//單播委託

//action2();

//單播委託

//action3();

//單播委託

console.readline();}}

class

student

public consolecolor pencolor

public

void

dohomework()

doing homework hours

",this

.id,i);

thread.sleep(

1000

); }

}}

2.隱式非同步呼叫

class

program

;student stu2 = new student ;

student stu3 = new student ;

//直接呼叫,同步

action action1 = new action(stu1.dohomework);//

間接呼叫,同步

action action2 = new

action(stu2.dohomework);

action action3 = new

action(stu3.dohomework);

//action1 += action2;

//action1 += action3;

//action1();

//多播委託,根據封裝方法的順序

//間接呼叫,同步

//action1();

//單播委託

//間接呼叫,同步

//action2();

//單播委託

//action3();

//單播委託

//委託的非同步呼叫

//隱式間接的非同步呼叫,會生成乙個分支方法,兩個引數第乙個引數是**方法,只子執行緒執行完,需要執行什麼東西

null);

////

action3.begininvoke(null, null);

//一般的多執行緒

thread thread1 = new thread(new threadstart(stu1.dohomework));//

顯式的直接的非同步呼叫,

thread thread2 = new thread(new

threadstart(stu2.dohomework));

thread thread3 = new thread(new

threadstart(stu3.dohomework));

thread1.start();

thread2.start();

thread3.start();

//高階一點帶委託的多執行緒

task task1 = new task(new action(stu1.dohomework));//

顯式的非同步呼叫

task task2 = new task(new

action(stu2.dohomework));

task task3 = new task(new

action(stu3.dohomework));

task1.start();

task2.start();

task3.start();

for (int i = 0; i < 10;i++)

",i);

thread.sleep(

1000

); }

console.readline();}}

class

student

public consolecolor pencolor

public

void

dohomework()

doing homework hours

",this

.id,i);

thread.sleep(

1000

); }}

}

應當適時使用介面取代對委託的使用

jQuery 高階事件 委託

function 使用live繫結的是document,而非button 所以,永遠只會繫結一次事件 button live click function bind 無法動態繫結事件 button bind click function live 可以動態繫結事件,因為事件繫結在document上 ...

委託的使用

using system using system.collections.generic using system.text namespace delegate private static void chinesegreeting string name 注意此方法,它接受乙個greeting...

委託的使用

委託字面上的理解就是讓別人做事,程式上的委託也差不多這個意思,不過委託的實現需要的步驟,體現的每詞實現委託的過程中 第一步 委託的宣告 新增乙個例項的委託 public delegate void clientpipelineobjectmodules request request 這個就是新增了...