老话题:主人、猫和老鼠
昨天朋友去面试,遇到了一个很常见的C#笔试题。发现现在好多公司都是在网上随便找点题来笔试。
以前看到过的一种方法,没有时间查了,凭印象写的,谁写的如果看到,确认一下。
以前看到过的一种方法,没有时间查了,凭印象写的,谁写的如果看到,确认一下。
using System;
using System.Collections.Generic;
using System.Text;
namespace CatAndMouse
{
public delegate void CatEventHandle(int score);
class Sleeping
{
public event CatEventHandle CatEvent;
private int intScore;
public int IntScore
{
get
{
return intScore;
}
set
{
if (!int.Equals(intScore, value))
{
CatEvent(value);
}
else
{
intScore = value;
}
}
}
}
class Mouse
{
public Mouse(Sleeping slp)
{
slp.CatEvent += new CatEventHandle(slp_CatEvent);
}
void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("老鼠:猫叫了,快逃命");
}
else
{
Console.WriteLine("系统:猫叫了,老鼠没有听到");
}
}
}
class Master
{
public Master(Sleeping slp)
{
slp.CatEvent += new CatEventHandle(slp_CatEvent);
}
void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("主人:猫叫了,去看看");
}
else
{
Console.WriteLine("系统:猫叫了,主人没有听到");
}
}
}
class Program
{
static void Main(string[] args)
{
Sleeping slp = new Sleeping();
Mouse mou = new Mouse(slp);
Master mas = new Master(slp);
int i = 1;
while (i <= 50)
{
Console.WriteLine("*********************************************");
Console.WriteLine("*\t" + i.ToString() + "\t分贝");
Console.WriteLine("*********************************************");
slp.IntScore = i;
i++;
}
Console.Read();
}
}
}
今天有点休息时间随便又写了一下,几分钟的时间,所以不对请批评:)统统接受。命名没有太注意,见谅。using System.Collections.Generic;
using System.Text;
namespace CatAndMouse
{
public delegate void CatEventHandle(int score);
class Sleeping
{
public event CatEventHandle CatEvent;
private int intScore;
public int IntScore
{
get
{
return intScore;
}
set
{
if (!int.Equals(intScore, value))
{
CatEvent(value);
}
else
{
intScore = value;
}
}
}
}
class Mouse
{
public Mouse(Sleeping slp)
{
slp.CatEvent += new CatEventHandle(slp_CatEvent);
}
void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("老鼠:猫叫了,快逃命");
}
else
{
Console.WriteLine("系统:猫叫了,老鼠没有听到");
}
}
}
class Master
{
public Master(Sleeping slp)
{
slp.CatEvent += new CatEventHandle(slp_CatEvent);
}
void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("主人:猫叫了,去看看");
}
else
{
Console.WriteLine("系统:猫叫了,主人没有听到");
}
}
}
class Program
{
static void Main(string[] args)
{
Sleeping slp = new Sleeping();
Mouse mou = new Mouse(slp);
Master mas = new Master(slp);
int i = 1;
while (i <= 50)
{
Console.WriteLine("*********************************************");
Console.WriteLine("*\t" + i.ToString() + "\t分贝");
Console.WriteLine("*********************************************");
slp.IntScore = i;
i++;
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace CatAndMouse_New
{
public delegate void CatEventHandler(int score);
class Program
{
static void Main(string[] args)
{
int i = 1;
while(i <= 50)
{
Console.WriteLine("*********************************************");
Console.WriteLine("*\t" + i.ToString() + "\t分贝");
Console.WriteLine("*********************************************");
CatEventHandler ceh = new CatEventHandler(new Mouse().slp_CatEvent);
CatEventHandler ceh1 = new CatEventHandler(new Master().slp_CatEvent);
CatEventHandler ceh2 = ceh + ceh1;
ceh2(i);
i++;
}
Console.Read();
}
}
abstract class CatEvent
{
public abstract void slp_CatEvent(int score);
}
class Mouse : CatEvent
{
public override void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("老鼠:猫叫了,快逃命");
}
else
{
Console.WriteLine("系统:猫叫了,老鼠没有听到");
}
}
}
class Master : CatEvent
{
public override void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("主人:猫叫了,去看看");
}
else
{
Console.WriteLine("系统:猫叫了,主人没有听到");
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace CatAndMouse_New
{
public delegate void CatEventHandler(int score);
class Program
{
static void Main(string[] args)
{
int i = 1;
while(i <= 50)
{
Console.WriteLine("*********************************************");
Console.WriteLine("*\t" + i.ToString() + "\t分贝");
Console.WriteLine("*********************************************");
CatEventHandler ceh = new CatEventHandler(new Mouse().slp_CatEvent);
CatEventHandler ceh1 = new CatEventHandler(new Master().slp_CatEvent);
CatEventHandler ceh2 = ceh + ceh1;
ceh2(i);
i++;
}
Console.Read();
}
}
abstract class CatEvent
{
public abstract void slp_CatEvent(int score);
}
class Mouse : CatEvent
{
public override void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("老鼠:猫叫了,快逃命");
}
else
{
Console.WriteLine("系统:猫叫了,老鼠没有听到");
}
}
}
class Master : CatEvent
{
public override void slp_CatEvent(int score)
{
if (score >= 30)
{
Console.WriteLine("主人:猫叫了,去看看");
}
else
{
Console.WriteLine("系统:猫叫了,主人没有听到");
}
}
}
}