Program Suggestion: All the mice started to escape, and the master was awakened. (C # Statement)
Requirements:
1. The behavior of a mouse and a host must be active.
2. The test token is scalable. the token is called Token, which may cause other dynamic effects.
Most of the answers are using event programming. I have changed my mind here, using the observation mode, and using the interface, because considering the second requirement, that is to say, the cat may also directly cause the host to wake up, so man also inherits the icatcatcher interface.
Source code As follows:
Using system;
Using system. collections;
Namespace Test
{
Public interface icatcatcher
{
Void dosomething ();
}
Public interface icatsubject
{
Void regesitercatcatcher (icatcatcher catcatcher );
Void miao ();
}
Public interface iratsubject
{
Void regesiterratcatcher (iratcatcher Ratcatcher );
Void run ();
}
Public interface iratcatcher
{
Void wake ();
}
Public class Cat: icatsubject
{
Public CAT ()
{
}
private arraylist catcherlist = new arraylist ();
Public void regesitercatcatcher (icatcatcher catcher)
{< br>
catcherlist. add (catcher);
}< br>
Public void miao ()
{< br>
console. writeline ("Miao");
for (INT I = 0; I
{< br>
icatcatcher catcatcatcher = (icatcatcher) catcherlist ;
catcatcher. dosomething ();
}< br>
[stathread]
Public static void main ()
{< br>
CAT = new CAT ();
Rat [] rat = new rat [10];
For (INT I = 0; I <10; I ++)
{
Rat=New rat (CAT );
}
Man man = new man (rat, CAT );
Cat. Miao ();
}
}
Public class rat: icatcatcher, iratsubject
{
Public rat (icatsubject catsub)
{
Catsub. regesitercatcatcher (this );
}
Public void dosomething ()
{
Run ();
}
Private arraylist ratcherlist = new arraylist ();
Public void regesiterratcatcher (iratcatcher catcher)
{
Ratcherlist. Add (catcher );
}
Public void run ()
{
Console. writeline ("rat run ");
For (INT I = 0; I <ratcherlist. Count; I ++)
{
Iratcatcher Ratcatcher = (iratcatcher) ratcherlist ;
Ratcatcher. Wake ();
}
}
}
Public class man: icatcatcher, iratcatcher
{
Public man (iratsubject [] ratsub, icatsubject catsub)
{
For (INT I = 0; I <ratsub. length; I ++)
{
Ratsub . Regesiterratcatcher ( This );
}
Catsub. regesitercatcatcher (this );
}
Public void dosomething ()
{
Console. writeline ("cat bring on wake ");
}
Public void wake ()
{
Console. writeline ("rats bring on wake ");
}
}
}
If debugging occurs, there will be a small problem, that is, there are many mice, and each mouse's run will have a wake man, so the host is awakened multiple times, in fact, this is only because the computer always executes the program in order, and the simulation of this effect should already be deemed to be in line with the meaning of the question.
If debugging occurs, there will be a small problem, that is, there are many mice, and each mouse's run will have a wake man, so the host is awakened multiple times, in fact, this is only because the computer always executes the program in order, and the simulation of this effect should already be deemed to be in line with the meaning of the question.