A cat and mouse wake the master's pen test (C #)

xiaoxiao2021-03-06  19

9cbs Post: http://community.9cbs.net/expert/topic/3839/3839240.xml? Temp = .607937

7. Program design: The cat is called, all the mice began to run away, the owner was awakened. (C # language) requirements:

1. To be linked, the behavior of mice and the owner is passive. 2. Consider scalability, the call of the cat can cause other linkage effects

Most of the answers are used in the event programming. I have changed my ideas here. I use the viewing mode. It is also implemented with the interface. Inherited ICATCATCHER interface

The source code is as follows:

Using system;

Using system.collections;

Namespace Test

{

Public Interface Icatcather

{

Void dosth ();

}

Public Interface Icatsubject

{

Void Regesitercatcher (Icatcatcher Catcatcher);

Void mIAO ();

}

Public Interface Iratsubject

{

Void REGESITERRATCATCHER (IRATCATCHER RATCATCHER);

Void Run ();

}

Public Interface Iratcher

{

Void wake ();

}

Public class cat: Icatsubject

{

Public cat ()

{

}

Private arraylist catcherlist = new arraylist ();

Public void Regesitercatcatcher (Icatcatcher Catcher)

{

CatcherList.Add (catcher);

}

Public void miao ()

{

Console.writeline ("miao");

For (int i = 0; i

{

ICATCATCHER CATCATCHER = (ICATCATCHER) CatcherList [i];

Catcatcher.dosth ();

}

}

[Stathread]

Public static void

Main

()

{

Cat cat = new cat ();

Rat [] Rat = New Rat [10];

For (int i = 0; i <10; i )

{

RAT [I] = 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 dosth ()

{

Run ();

}

Private arraylist ratcherlist = new arraylist ();

Public void RegeSiterratcher (Iratcatcher catcher)

{

RatcherList.Add (catcher);

}

Public void Run ()

{

Console.writeline ("Rat Run");

For (int i = 0; i

Iratcher Ratcatcher = (Iratcatcher) RatcherList [i];

Ratcatcher.wake ();

}

}

}

Public Class Man: ICATCATCHER, IRATCATCHER

{

Public Man (Iratsubject [] RatSub, Icatsubject Catsub)

{

For (int i = 0; i

{

Ratsub [i] .regesiterratcatcher (this);

}

Catsub.RegeSitercatcatcher (this);

}

Public void dosth ()

{

Console.writeLine ("Cat Bring On Wake");

}

Public void wake ()

{

Console.WriteLine ("Rats Bring On Wake");

}

}

}

Here, if the debug will have a little problem, there are many mice, and the Run of each mouse will be Man MAN, so it feels that the owner is awakened many times. In fact, the computer always executes the program in order, and can simulate This effect should be considered in line with the meaning.

Here, if the debug will have a little problem, there are many mice, and the Run of each mouse will be Man MAN, so it feels that the owner is awakened many times. In fact, the computer always executes the program in order, and can simulate This effect should be considered in line with the meaning.

转载请注明原文地址:https://www.9cbs.com/read-48612.html

New Post(0)