有没高手帮忙解这道C#题,高手,高手,高手,高手~
有一个老鼠Mouse类和一个猫类Cat,通过委托与事件的机制,实现:当你在客户端输入Day时,表示白天到了,那么老鼠就要回到窝里去了。而猫则要起床了。当你在客户端输入Ni...
有一个老鼠Mouse类和一个猫类Cat,通过委托与事件的机制,实现:
当你在客户端输入Day时,表示白天到了,那么老鼠就要回到窝里去了。而猫则要起床了。
当你在客户端输入Night时,表示黑夜到了,那么猫就要到被窝里睡觉了。而老鼠则准备出来活动,吃点东西了。 展开
当你在客户端输入Day时,表示白天到了,那么老鼠就要回到窝里去了。而猫则要起床了。
当你在客户端输入Night时,表示黑夜到了,那么猫就要到被窝里睡觉了。而老鼠则准备出来活动,吃点东西了。 展开
展开全部
/// <summary>
/// 老鼠类 有睡觉和起床俩个方法
/// </summary>
public class Mouse
{
public void Sleep()
{}
public void GetUp()
{}
}
/// <summary>
/// 猫类 有睡觉和起床两个方法
/// </summary>
public class Cat
{
public void Sleep()
{ }
public void GetUp()
{ }
}
//声明委托 参数为输入字符串 无返回
public delegate void DoSomething(string inputStr);
//实现委托的方法
public void SleepOrGetup(string inputStr)
{
Mouse m = new Mouse();
Cat c = new Cat();
if (inputStr == "Day")
{
m.Sleep();
c.GetUp();
}
else if (inputStr == "Night")
{
m.GetUp();
c.Sleep();
}
}
//定义事件 当客户端输入时
public event DoSomething Input;
//测试
public void Test()
{
Input += new DoSomething(SleepOrGetup);
Input("Day");
}
/// 老鼠类 有睡觉和起床俩个方法
/// </summary>
public class Mouse
{
public void Sleep()
{}
public void GetUp()
{}
}
/// <summary>
/// 猫类 有睡觉和起床两个方法
/// </summary>
public class Cat
{
public void Sleep()
{ }
public void GetUp()
{ }
}
//声明委托 参数为输入字符串 无返回
public delegate void DoSomething(string inputStr);
//实现委托的方法
public void SleepOrGetup(string inputStr)
{
Mouse m = new Mouse();
Cat c = new Cat();
if (inputStr == "Day")
{
m.Sleep();
c.GetUp();
}
else if (inputStr == "Night")
{
m.GetUp();
c.Sleep();
}
}
//定义事件 当客户端输入时
public event DoSomething Input;
//测试
public void Test()
{
Input += new DoSomething(SleepOrGetup);
Input("Day");
}
展开全部
神简单
追问
能不能发下代码,谢谢哈
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static Time time = new Time();
static Cat cat = new Cat();
static Mouse mouse = new Mouse();
delegate void DoFun(object sender, EventArgs e);
static void Main(string[] args)
{
Console.WriteLine("please input the time:");
while (true)
{
switch (Console.ReadLine())
{
case "Day":
ClearEvent();
Time.timeEvent += new EventHandler(cat.WakeUp);
Time.timeEvent += new EventHandler(mouse.GotoHole);
time.timeCome("Day");
break;
case "Night":
ClearEvent();
Time.timeEvent += new EventHandler(cat.Sleep);
Time.timeEvent += new EventHandler(mouse.MouseActity);
time.timeCome("Night");
break;
case "exit":
return;
default:
Console.WriteLine("error input");
break;
}
}
}
static void ClearEvent()
{
Time.timeEvent -= new EventHandler(cat.Sleep);
Time.timeEvent -= new EventHandler(mouse.MouseActity);
Time.timeEvent -= new EventHandler(cat.WakeUp);
Time.timeEvent -= new EventHandler(mouse.GotoHole);
}
}
//public delegate void timeEventHandler();
public class Time
{
public static event EventHandler timeEvent;
public void timeCome(string time)
{
Console.WriteLine("the time is:" + time);
EventArgs e = new EventArgs();
timeEvent(this, e);
}
}
public class Cat
{
public void WakeUp(object sender, EventArgs e)
{
Console.WriteLine("the cat wake up");
}
public void Sleep(object sender, EventArgs e)
{
Console.WriteLine("the cat sleep");
}
}
public class Mouse
{
public void GotoHole(object sender, EventArgs e)
{
Console.WriteLine("the Mouse is goto hole");
}
public void MouseActity(object sender, EventArgs e)
{
Console.WriteLine("the Mouse is Activity,it want to eat something!");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static Time time = new Time();
static Cat cat = new Cat();
static Mouse mouse = new Mouse();
delegate void DoFun(object sender, EventArgs e);
static void Main(string[] args)
{
Console.WriteLine("please input the time:");
while (true)
{
switch (Console.ReadLine())
{
case "Day":
ClearEvent();
Time.timeEvent += new EventHandler(cat.WakeUp);
Time.timeEvent += new EventHandler(mouse.GotoHole);
time.timeCome("Day");
break;
case "Night":
ClearEvent();
Time.timeEvent += new EventHandler(cat.Sleep);
Time.timeEvent += new EventHandler(mouse.MouseActity);
time.timeCome("Night");
break;
case "exit":
return;
default:
Console.WriteLine("error input");
break;
}
}
}
static void ClearEvent()
{
Time.timeEvent -= new EventHandler(cat.Sleep);
Time.timeEvent -= new EventHandler(mouse.MouseActity);
Time.timeEvent -= new EventHandler(cat.WakeUp);
Time.timeEvent -= new EventHandler(mouse.GotoHole);
}
}
//public delegate void timeEventHandler();
public class Time
{
public static event EventHandler timeEvent;
public void timeCome(string time)
{
Console.WriteLine("the time is:" + time);
EventArgs e = new EventArgs();
timeEvent(this, e);
}
}
public class Cat
{
public void WakeUp(object sender, EventArgs e)
{
Console.WriteLine("the cat wake up");
}
public void Sleep(object sender, EventArgs e)
{
Console.WriteLine("the cat sleep");
}
}
public class Mouse
{
public void GotoHole(object sender, EventArgs e)
{
Console.WriteLine("the Mouse is goto hole");
}
public void MouseActity(object sender, EventArgs e)
{
Console.WriteLine("the Mouse is Activity,it want to eat something!");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询