.


:




:

































 

 

 

 


2. Java. 2

2

1. #

using System;

 

 

//

delegate void DelF ();

 

class TestDel

{

void F (){Console.WriteLine ("F");}

static void Main ()

{

// TestDel

TestDel tD= new TestDel();

 

// dF DelF F()

DelF dF= new DelF (tD.F);

 

// dF

dF();

}

}

 

1) . , . .

, ,

)

using System;

 

//

delegate void DelF ();

 

class A

{

public DelF delF=null; //

}

 

class B

{

public void F (){Console.WriteLine ("F");}

static void Main ()

{

B b= new B();

// F() B

b.F();

// a A

A a= new A();

// a.delF F() B

a.delF= new DelF(b.F);

// delF

a.delF();

}

}

 

)

using System;

 

//

delegate void DelF ();

 

class A

{

public DelF delF; //

public A()

{ //

delF= new DelF(this.F);

}

//

public void F (){Console.WriteLine ("F");}

}

class B

{

static void Main ()

{

A a= new A(); // a A

// delF

a.delF();

}

}

 

)

using System;

 

//

delegate void DelF ();

 

class A

{

public DelF delF=null; //

public A(DelF d)

{ // delF

//

delF+= d;

}

}

 

class B

{

public void F (){Console.WriteLine ("F");}

static void Main ()

{

B b= new B();

DelF dF= new DelF(b.F);

// a A

A a= new A(dF);

// delF

a.delF();

}

}

 

.//////////////

using System;

 

//

delegate int DelRet (int x, int y);

delegate void DelRef (int x, int y, ref int z);

 

class TestDel

{

static void Main ()

{

TestDel tD= new TestDel(); // TestDel

 

// DelRet SumRet

DelRet tRet= new DelRet (tD.SumRet);

// tRet

Console.WriteLine ("4+5= " + tRet(4, 5));

//DelRet tRet1= new DelRet(tD.SumRef); //

 

// DelRef SumRef

DelRef tRef= new DelRef (tD.SumRef);

// tRet

int z=0;

tRef (10, 20, ref z);

Console.WriteLine ("10+20= " + z);

 

// UseDel DelRet DelRef

Console.WriteLine ("(11+12)+(100+200)= " + tD.UseDel(tRet, tRef));

}

 

int SumRet (int a, int b){return a+b;}

 

void SumRef (int a, int b, ref int c){c= a+b;}

 

int UseDel (DelRet dR, DelRef dF)

{

int tmp= 0;

dF (11,22, ref tmp);

return tmp+dR (100,200);

}

}

 

 

1. ( )

using System;

 

//

delegate void DelEv (int x, string s);

 

class TestEv

{

public event DelEv ev; //

 

static void Main ()

{

TestEv tE= new TestEv (); // TestEv

// ev

tE.ev +=new DelEv (tE.Handler);

//

tE.ev (4,"four");

}

 

//

void Handler (int a, string b)

{

Console.WriteLine ("a= " + a + " b= "+ b);

}

}

 

2. ( )

 

 

using System;

 

//

delegate void DelEv (int x, string s);

 

class Source //

{

public event DelEv ev; //

 

//

public void Fire (int x, string s)

{

if(ev!= null) ev (x, s);

}

}

 

class Receiver //

{

//

public void Handler (int a, string b)

{

Console.WriteLine ("a= " + a + " b= "+ b);

}

}

 

class TestEv

{

static void Main ()

{

Source s= new Source (); //

Receiver r= new Receiver (); //

// ev

s.ev +=new DelEv (r.Handler);

//

s.Fire (5,"five");

}

}

 

 

 

using System;

 

//

delegate void DelEv (int x, string s);

 

class OurEventArgs: EventArgs //

{

private int x;

private String s;

 

public OurEventArgs (int X, String S)

{x= X; s= S;}

 

public int X {get{return x;}}

public String S{get{return s;}}

}

 

class Source

{

public event EventHandler ev; //

 

//

public void Fire (int x, string s)

{

//

OurEventArgs oE= new OurEventArgs (x, s);

//

if(ev!= null) ev (this, oE);

}

}

 

class Receiver

{

//

public void Handler (object sender, EventArgs oE)

{

//

OurEventArgs oea= (OurEventArgs)oE;

Console.WriteLine ("a= " + oea.X + " b= "+ oea.S);

}

}

 

class TestEv

{

static void Main ()

{

Source s= new Source (); //

Receiver r= new Receiver (); //

// ev

s.ev +=new EventHandler (r.Handler);

//

s.Fire (5,"five");

}

}

 

2) . , .

 

3) . .

 

4) . .

 

5) . .

 

6) . .

 

7) , -, , .

 

2. Java



<== | ==>
| 
:


: 2016-09-03; !; : 221 |


:

:

, .
==> ...

1697 - | 1506 -


© 2015-2024 lektsii.org - -

: 0.033 .