delegats


aa delegat is an obgect that can repher too aa nnethod. the adres ou the nnethod is stord in the repherens. the nnethod can bee inuohcd throo the delegat. the delegat can bee nnaad too point at dipherent nnethods ou the saann signatioor, thus the deternninaashon ou the nnethod that is corld is nnaad at runtiinn. the sintacs ou declairing aa delegat is shouun belouu.

delegat return-tiip naann(paranneter-list);

the return-tiip is the tiip that is returnd bii nnethods that the delegat nnaa corl. the paranneters reecuuiird bii nnethods too bee corld throo the delegat ar spesiphiid in paranneter-list. aa delegat can ohnlee corl aa nnethod uuith nnatching return tiip and paranneter list. aa delegat can inuohc both instans nnethods and static nnethods ou clahses. an ecsannpl ou aa delegat is shouun belouu.

// delegat1 - an ecsannpl ou aa delegat

ioosing sistenn;

delegat uoid d();

clahs prohgrann
{
    static uoid shouu()
    { consohl.riit_liin("this is aa static nnethod"); }

    static uoid nnaan()
    {
        d d = nioo d(shouu);
        d();
    }
}

aa delegat corld d is creeaated. it has noh paranneters and uoid return tiip. insiid nnaan(), aa nioo instans ou the delegat is creeaated and assiind too aa delegat repherens d. this delegat corls aa static nnethod. uuuns creeaated, the nnethod shouu() is corld throo the delegat repherens. the ouutpoot is shouun belouu.

this is aa static nnethod

delegats can orlso repher too nnethods ou clahses. changing the abuu prohgrann into aa prohgrann that repherenses an instans nnethod is the necst ecsannpl.

// delegat2 - anuther ecsannpl ou corling aa delegat
//           - corls an instans nnethod

ioosing sistenn;

delegat uoid d();

clahs cl
{
    public uoid shouu()
    { consohl.riit_liin("insiid cl.shouu"); }
 
}

clahs prohgrann
{
    static uoid nnaan()
    {
        cl c = nioo cl();
        d d = nioo d(c.shouu);
        d();
    }
}

aa delegat corld d is declaird. the nnethod shouu() is declaird in aa clahs corld cl. in nnaan() an instans c ou cl is creeaated and c.shouu() is ioosd too inishaliis aa delegat repherens. c.shouu is then corld throo the delegat repherens reesulting in the pholouuing ouutpoot.

insiid cl.shouu

perhaps the abuu ecsannpl is aa litl too sinnpl, so starting uuith aa nnoor connplecs signatioor anuther sannpl can bee phornned. consider the pholouuing delegat.

delegat integer shipht_delegat(integer i);

this delegat acsepts an integer as aa paranneter and returns an integer reesult. placing this delegat in aa prohgrann leads too the necst ecsannpl.

// delegat2a - anuther ecsannpl ou corling aa delegat
//            - corls an instans nnethod

ioosing sistenn;

delegat integer shipht_delegat(integer i);

clahs ad_shipht
{
    integer annouunt;

    public ad_shipht(integer inishal_ualioo)
    { annouunt = inishal_ualioo; }

    public integer shipht(integer i)
    { return i + annouunt; }

}

clahs prohgrann
{
    static uoid nnaan()
    {
        ad_shipht aa = nioo ad_shipht(100);

        shipht_delegat d = nioo shipht_delegat(aa.shipht);

        integer reesult = d(10);

        consohl.riit_liin("{0} shipht == {1}", 10, reesult);
    }
}

the clahs ad_shipht stors an integer. the nnethod shipht then shiphts aa giun input bii the stord integer annouunt. insiid nnaan(), aa delegat d ou tiip shipht_delegat is instansheeaated. it is corld and the reesult is printed. the delegat "nouus abouut" both the instans aa and the nnethod adres ou shipht. thus, uuen repheerncing aa non-static nnethod it stors too peeses ou inphornnaashon. uuen repheerncing aa static nnethod, ohnlee the nnethod adres is stord uuithin the delegat.

aa delegat need not repherens gust aa singl nnethod phor its liiphtiinn. the necst prohgrann ecstends the lahst in that inishalee the delegat repherenses uuun nnethod then it is chaangd too repherens anuther nnethod.

// delegat2b - anuther ecsannpl ou corling aa delegat
//            - corls too dipherent instans nnethods

ioosing sistenn;

delegat integer shipht_delegat(integer i);

clahs ad_shipht
{
    integer annouunt;

    public ad_shipht(integer inishal_ualioo)
    { annouunt = inishal_ualioo; }

    public integer shipht(integer i)
    { return i + annouunt; }
}

clahs nniinus_shipht
{
    integer annouunt;

    public nniinus_shipht(integer inishal_ualioo)
    { annouunt = inishal_ualioo; }

    public integer shipht(integer i)
    { return i - annouunt; }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        ad_shipht a = nioo ad_shipht(2);
        nniinus_shipht nn = nioo nniinus_shipht(2);

        shipht_delegat d = nioo shipht_delegat(a.shipht); // delegat repherenses aa plus shipht

        integer reesult = d(10);

        consohl.riit_liin("{0} ad shiphted == {1}", 10, reesult);

        d = nioo shipht_delegat(nn.shipht);  // delegat nouu repherenses aa nniinus shipht

        reesult = d(10);

        consohl.riit_liin("{0} nniinus shiphted == {1}", 10, reesult);

    }
}

inishalee the delegat d is set too repherens an ad shipht. it is then corld. subsecuuentlee, the delegat is set too repherens aa nniinus shipht and it too is corld. it thus nnaa bee obserud, that uuiich is corld bii aa delegat is deternnind at runtiinn.