Event entrusting mouse and cat owners

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace TheStoryOfCat
{
Class Program
{
Static void Main (string [] args)
{
Cat cat = new Cat () {Name = "Petal "};
Mouse mouse = new Mouse () {Name = "Jack "};
Cat. CatMonitorEvent + = cat. Move;
Cat. CatMonitorEvent + = mouse. Move;
// Master
Master master = new Master (cat) {Name = "Old Boss "};
Cat. Run ();
Console. ReadKey ();
}
}

Public delegate void MonitorHandle ();
Public class Cat
{
Public string Name;

Public event MonitorHandle CatMonitorEvent;

Public Cat (){
}

Public void Move ()
{
Console. WriteLine ("Cat:" + this. Name + "Moving ");

}
Public void Run ()
{
If (CatMonitorEvent! = Null)
CatMonitorEvent ();
}
}
Public class Mouse
{
Public string Name;

Public Mouse (){
}

Public void Move ()
{
Console. WriteLine ("Mouse:" + this. Name + "Moving ");
}
}

Public class Master
{
Public string Name;

/// <Summary>
/// Injection
/// </Summary>
/// <Param name = "cat"> </param>
Public Master (Cat cat)
{
If (cat! = Null)
Cat. CatMonitorEvent + = Move;
}
Public void Move ()
{
Console. WriteLine ("Master:" + this. Name + "Moving ");
}
}
}

Statement 2:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;


Namespace TheStoryOfCat
{

Class Program
{
Static void Main (string [] args)
{
Cat cat = new Cat () {Name = "Petal "};
Mouse mouse = new Mouse () {Name = "Jack "};
Cat. CatMonitorEvent + = cat. Move;

Cat. CatMonitorEvent + = mouse. Move;
// Master
Master master = new Master (cat) {Name = "Old Boss "};
Cat. Run (new MonitorEventArgs (20 ));
Console. WriteLine ("---------- gorgeous split line -------------");
Cat. Run (new MonitorEventArgs (70 ));
Console. ReadKey ();
}
}
Public class MonitorEventArgs: EventArgs
{
Public int Sound;
Public MonitorEventArgs (int _ Sound ){
This. Sound = _ Sound;
}
}

Public delegate void MonitorHandle (object obj, MonitorEventArgs arg );
/// <Summary>
/// Cat
/// </Summary>
Public class Cat
{
Public string Name;

Public event MonitorHandle CatMonitorEvent;

Public Cat (){
}

Public void Move (object obj, MonitorEventArgs arg)
{
Console. WriteLine ("{0} current call {1} decibel", this. Name, arg. Sound );
}
Public virtual void Run (MonitorEventArgs arg)
{
If (CatMonitorEvent! = Null)
CatMonitorEvent (this, arg );
}
}
/// <Summary>
/// Mouse
/// </Summary>
Public class Mouse
{
Public string Name;

Public Mouse (){
}
/// <Summary>
/// The call of a cat is not expected if it is lower than 50 decibels
/// </Summary>
/// <Param name = "obj"> </param>
/// <Param name = "arg"> </param>
Public void Move (object obj, MonitorEventArgs arg)
{
If (arg = null)
Return;
String result = "unexpected ";
If (arg. Sound> 50)
Result = "run ";
Console. WriteLine ("{0} Heard, current cat call {1} decibel, {2}", this. Name, arg. Sound, result );
}
}
/// <Summary>
/// Master
/// </Summary>
Public class Master
{
Public string Name;

/// <Summary>
/// Injection
/// </Summary>
/// <Param name = "cat"> </param>
Public Master (Cat cat)
{
If (cat! = Null)
Cat. CatMonitorEvent + = Move;
}
Public void Move (object obj, MonitorEventArgs arg)
{
If (arg = null)
Return;
String result = "Cats lack of air ";
If (arg. Sound> 50)
Result = "the cat is in good state ";
Console. WriteLine ("host {0}, heard the current call of the cat {1} decibel, {2}", this. Name, arg. Sound, result );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.