设计模式—观察者模式
(猫捉老鼠)
利用多播委托的方法,把猫行动的方法广播给所有的老鼠
老鼠生成的时候就添加自己的移动方法到委托里,在猫移动的时候直接调用委托,老鼠在委托里的方法就会被调用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace learn
{
class Program
{
static void Main(string[] args)
{
Cat newCat = new Cat("JeF", "Blue");
Mouse[] mouses = new Mouse[100];
int i = 0;
Console.WriteLine("添加老鼠");
while (true)
{
string MouseName;
string MouseColor;
Console.Write("Name:");
MouseName = Console.ReadLine();
Console.Write("Color:");
MouseColor = Console.ReadLine();
mouses[i] = new Mouse(MouseName, MouseColor);
newCat.CatCome += mouses[i].MouseRun;//添加多播委托,让老鼠接受到委托消息,监控猫类的行动
Console.WriteLine("是否继续添加(Y/N)?:");
string ret = Console.ReadLine();
if (ret == "N")
{
break;
}
}
while (true)
{
Console.WriteLine("----------------");
Console.WriteLine("--1.猫抓老鼠----");
Console.WriteLine("--2.没有行动----");
Console.WriteLine("----------------");
int choose = Convert.ToInt32(Console.ReadLine());
switch (choose)
{
case 1:
newCat.CatComing();
Thread.Sleep(2000);
break;
case 2:
Console.WriteLine("猫没有任何行动");
break;
}
Console.Clear();
}
}
}
class Cat
{
private string name;
private string color;
public Cat(string name, string color)
{
this.Name = name;
this.Color = color;
}
public string Name { get => name; private set => name = value; }
public string Color { get => color; private set => color = value; }
public void CatComing()
{
Console.WriteLine("一只猫进来了\nName:" + name + "\nColor:" + color);
if (CatCome != null)
CatCome();//调用委托
}
public Action CatCome;
}
class Mouse
{
private string name;
private string color;
public Mouse(string name, string color)
{
this.name = name;
this.color = color;
}
public string Name { get => name; private set => name = value; }
public string Color { get => color; private set => color = value; }
public void MouseRun()
{
Console.WriteLine("老鼠逃跑了\nName:" + name + "\nColor:" + color);
}
}
}
委托和事件的区别
添加事件,使该委托不会在类外被直接使用但可以被注册然后增减方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn
{
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat("Cat1", "Blue");
Mouse[] mouses = new Mouse[]
{
new Mouse("mouse1", "Blue",cat),
new Mouse("mouse2","White",cat)
};
cat.CatComing();
}
}
class Mouse
{
private string name;
private string color;
public Mouse(string name, string color, Cat cat)
{
this.name = name;
this.color = color;
cat.Catcome += this.MouseRun;//在构造函数里添加被广播的方法
}
public void MouseRun()
{
Console.WriteLine("老鼠跑走了");
}
}
class Cat
{
private string name;
private string color;
public Cat(string name, string color)
{
this.name = name;
this.color = color;
}
public void CatComing()
{
Console.WriteLine("猫来了");
Catcome();
}
public event Action Catcome;//添加事件,使该委托不会被直接在类外使用但可以被访问增减方法
}
}
数据的初始化和LINQ的第一个基础使用
与SQL语法相似
//
//string.Format()方法,用于将一段字符串插入另一段字符串
string s = string.Format("插入字符:{0}","被插入的字符");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn
{
class LINQ
{
//用于数据查询
static void Main(string[] args)
{
var pelList = new List<People>()
{
new People(){ Id = 1,Name="per1",Age=12,Skill="skill1",Level=2},
new People(){ Id = 2,Name="per2",Age=22,Skill="skill2",Level=7},
new People(){ Id = 3,Name="per3",Age=52,Skill="skill3",Level=1},
new People(){ Id = 4,Name="per4",Age=11,Skill="skill2",Level=4},
new People(){ Id = 5,Name="per5",Age=18,Skill="skill1",Level=2}
};
var SkillList = new List<Skill>()
{
new Skill(){Id=1 ,SkillName="skill1",SkillPower=8 },
new Skill(){Id=2 ,SkillName="skill2",SkillPower=1 },
new Skill(){Id=3 ,SkillName="skill3",SkillPower=5 }
};
//查询\
var res = new List<People>();
//定义一个List集合存放
foreach (var temp in pelList)
{
if (temp.Level == 2)
{
res.Add(temp);
}
}
//foreach (var te in res)
//{
// Console.WriteLine(te);
//}
var rest = from m in pelList
where m.Level == 2
select m;
//表示把m的集合返回
//代表在pelList中做查询,m是其每个元素
//单独使用m的时候,没有指定元素,默认仍是ToString(),所以需要重写。
foreach (var n in rest)
{
Console.WriteLine(n);
}
}
}
class People
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Skill { get; set; }
public int Level { get; set; }
public override string ToString()
{
return string.Format("Id:{0} Name:{1} Age:{2} Skill:{3} Level:{4}", Id, Name, Age, Skill, Level);
//string.Format方法作用把多个字符串组合在一起
}
}
class Skill
{
public int Id { get; set; }
public string SkillName { get; set; }
public int SkillPower { get; set; }
}
}
from m in pelList 在pelList中做查询,m代表每一条数据
where m.Level == 2 查询所有Level等于2的数据
select m; 返回m,如果需要特定的返回值可以写m.Name等,反之单独情况下需要重写ToString()方法
LINQ扩展写法
使用list.where方法来选择数据
可以看作一个过滤器,需要为这个过滤器写一个规则,通过遍历加返回布尔值判断当前数字是否符合规则并筛选。
where会自动遍历。
使用Lambda表达式可以简写代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace learn
{
class LINQ
{
//用于数据查询
static void Main(string[] args)
{
var pelList = new List<People>()
{
new People(){ Id = 1,Name="per1",Age=12,Skill="skill1",Level=2},
new People(){ Id = 2,Name="per2",Age=22,Skill="skill2",Level=7},
new People(){ Id = 3,Name="per3",Age=52,Skill="skill3",Level=1},
new People(){ Id = 4,Name="per4",Age=11,Skill="skill2",Level=4},
new People(){ Id = 5,Name="per5",Age=18,Skill="skill1",Level=2}
};
var SkillList = new List<Skill>()
{
new Skill(){Id=1 ,SkillName="skill1",SkillPower=8 },
new Skill(){Id=2 ,SkillName="skill2",SkillPower=1 },
new Skill(){Id=3 ,SkillName="skill3",SkillPower=5 }
};
//查询\
var res2 = pelList.Where(ret);
//可以理解为过滤,需要写一个静态bool类型的方法作为过滤的规则
foreach (var n in res2)
{
Console.WriteLine(n);
}
}
//作为where方法的过滤规则
static bool ret(People pel)
{
if (pel.Level == 2) return true;
return false;
}
}
并列查询在判断语句后添加&&即可