添加Lesson12
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,117 @@
|
||||
namespace Lesson12_委托练习
|
||||
{
|
||||
#region 第一题
|
||||
abstract class Person()
|
||||
{
|
||||
public abstract void Eating();
|
||||
}
|
||||
|
||||
class Mother : Person
|
||||
{
|
||||
public Action beginEat;
|
||||
|
||||
public void Cooking()
|
||||
{
|
||||
Console.WriteLine("妈妈做饭");
|
||||
if (beginEat != null)
|
||||
{
|
||||
beginEat();
|
||||
}
|
||||
}
|
||||
public override void Eating()
|
||||
{
|
||||
Console.WriteLine("妈妈吃饭");
|
||||
|
||||
}
|
||||
}
|
||||
class Father : Person
|
||||
{
|
||||
public override void Eating()
|
||||
{
|
||||
Console.WriteLine("爸爸吃饭");
|
||||
}
|
||||
}
|
||||
class Son : Person
|
||||
{
|
||||
public override void Eating()
|
||||
{
|
||||
Console.WriteLine("孩子吃饭");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
class Monster
|
||||
{
|
||||
//当怪物死亡时,把自己作为参数传出去
|
||||
public Action<Monster> deadDoSomething;
|
||||
public int money = 10;
|
||||
public void Dead()
|
||||
{
|
||||
Console.WriteLine("怪物死亡");
|
||||
if (deadDoSomething != null)
|
||||
{
|
||||
deadDoSomething(this);
|
||||
}
|
||||
|
||||
deadDoSomething = null;//清空所需要做的事情(在这里就是怪物的击杀奖励已经领取了不要重复刷
|
||||
//一般有加就有减
|
||||
}
|
||||
}
|
||||
class Player
|
||||
{
|
||||
private int money = 0;
|
||||
public void MonsterDeadDoSomething(Monster m)
|
||||
{
|
||||
this.money += m.money;
|
||||
Console.WriteLine("玩家获得了" + m.money + "金币");
|
||||
}
|
||||
}
|
||||
class Panel
|
||||
{
|
||||
private int showNowMoney = 0;
|
||||
public void MonsterDeadDoSomething(Monster m)
|
||||
{
|
||||
this.showNowMoney += m.money;
|
||||
Console.WriteLine("面板显示当前金币" + showNowMoney);
|
||||
}
|
||||
}
|
||||
class Achievement
|
||||
{
|
||||
private int nowKillMonsterNum = 0;
|
||||
public void MonsterDeadDoSomething(Monster m)
|
||||
{
|
||||
nowKillMonsterNum += 1;
|
||||
Console.WriteLine("当前击杀了" + nowKillMonsterNum + "个怪物");
|
||||
}
|
||||
}
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
#region 第一题
|
||||
Mother mother = new Mother();
|
||||
Father father = new Father();
|
||||
Son son = new Son();
|
||||
|
||||
mother.beginEat += father.Eating;
|
||||
mother.beginEat += son.Eating;
|
||||
mother.beginEat += mother.Eating;
|
||||
|
||||
mother.Cooking();
|
||||
Console.WriteLine("_______________________");
|
||||
#endregion
|
||||
|
||||
Monster m = new Monster();
|
||||
Player p = new Player();
|
||||
Panel panel = new Panel();
|
||||
Achievement achievement = new Achievement();
|
||||
|
||||
m.deadDoSomething += p.MonsterDeadDoSomething;
|
||||
m.deadDoSomething += panel.MonsterDeadDoSomething;
|
||||
m.deadDoSomething += achievement.MonsterDeadDoSomething;
|
||||
m.Dead();
|
||||
m.Dead();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user