老鼠\猫\狗\主人晚上 老鼠出来 被猫发现 猫抓 老鼠
这时 狗 追猫
主人被惊醒 打了狗
用C#写出程序 要求是 连动效果我觉得 应该很简单 但是 唉!!!!
这时 狗 追猫
主人被惊醒 打了狗
用C#写出程序 要求是 连动效果我觉得 应该很简单 但是 唉!!!!
解决方案 »
- 如何重置datagridview
- 对十个textbox控件的有效性验证
- 求助datagrid或datagridview中怎样拖动行
- ZedGraph产生的临时文件如何删除???
- C# 如何实现歌词与播放同步
- 北京接收应届生的单位看过来(站出来!!!)(.net)
- 求 开源的 UML 绘制工具。或开源的C#的矢量图绘制工具,哪怕是一点点代码也好,可以放大/缩小/拖动 所绘的矢量图的
- 内存占用率太高!
- Session的奇怪现像!
- treeview节点的内容如何修改
- C#如何调用Active????
- 小弟初学C#,请各位热心的朋友不吝赐教:c#里用socket实现同https服务器的通信问题,分数不够可以再加。
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class Program
{
static Actor mouse;
static Actor cat;
static Actor dog;
static Actor master;
static void Main(string[] args)
{
//声明变量
mouse = new Actor("老鼠");
cat = new Actor("猫");
dog = new Actor("狗");
master = new Actor("主人"); //注册事件
mouse.BeginActEvent += new myDelegate(mouse_BeginActEvent);
cat.BeginActEvent += new myDelegate(cat_BeginActEvent);
dog.BeginActEvent += new myDelegate(dog_BeginActEvent); mouse.BeginAct("觅食"); Console.ReadLine();
} static void dog_BeginActEvent()
{
Console.WriteLine("主人打狗");
} static void cat_BeginActEvent()
{
dog.BeginAct("追猫");
} static void mouse_BeginActEvent()
{
cat.BeginAct("抓老鼠");
}
} public delegate void myDelegate();
public class Actor
{
public event myDelegate BeginActEvent; public Actor()
{
BeginActEvent = null;
} public Actor(string name) : this()
{
_Name = name;
} public void BeginAct(string strAction)
{
Console.WriteLine(_Name + strAction + "\n");
if (BeginActEvent != null)
{
BeginActEvent();
}
} private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
}这样可以么?
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class Program
{
static Actor mouse;
static Actor cat;
static Actor dog;
static Actor master;
static void Main(string[] args)
{
//声明变量
mouse = new Actor("老鼠");
cat = new Actor("猫");
dog = new Actor("狗");
master = new Actor("主人"); //注册事件
mouse.BeginActEvent += new myDelegate(mouse_BeginActEvent);
cat.BeginActEvent += new myDelegate(cat_BeginActEvent);
dog.BeginActEvent += new myDelegate(dog_BeginActEvent); mouse.BeginAct("觅食"); Console.ReadLine();
} static void dog_BeginActEvent(object sender)
{
Console.WriteLine("主人打" + (sender as Actor).Name);
} static void cat_BeginActEvent(object sender)
{
dog.BeginAct("追" + (sender as Actor).Name);
} static void mouse_BeginActEvent(object sender)
{
cat.BeginAct("抓" + (sender as Actor).Name);
}
} public delegate void myDelegate(object sender);
public class Actor
{
public event myDelegate BeginActEvent; public Actor()
{
BeginActEvent = null;
} public Actor(string name) : this()
{
_Name = name;
} public void BeginAct(string strAction)
{
Console.WriteLine(_Name + strAction + "\n");
if (BeginActEvent != null)
{
BeginActEvent(this);
}
} private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
}改了一下,这样更像 OO 了