也许尝试进一步推动你的作文。你只需要一种具有不同能力的类型,例如:
public class Hero
{
private readonly string name;
private readonly IEnumerable<IHeroAbility> abilities;
public Hero(string name, [Optional] IEnumerable<IHeroAbility> abilities)
{
this.name = name;
this.abilities = abilities ?? new List<IHeroAbility> { new OhCrap() };
}
public void Attack()
{
Console.WriteLine(name);
foreach (var ability in abilities) ability.Execute();
Console.WriteLine();
}
}
现在准备可用的能力:
public interface IHeroAbility
{
void Execute();
}
public class SwingSwordAbility : IHeroAbility
{
public void Execute()
{
Console.WriteLine("And with all his might the hero swung his sword down onto his opponent...");
}
}
public class ShootArrowAbility : IHeroAbility
{
public void Execute()
{
Console.WriteLine("The hero loads an arrow into his bow without blinking and launches it with fierce speed towards his opponant...");
}
}
public class CastFireBallAbility : IHeroAbility
{
public void Execute()
{
Console.WriteLine("The hero reaches deep into the underverse and hurls a ball of fire towards his opponant...");
}
}
public class OhCrap : IHeroAbility
{
public void Execute()
{
Console.WriteLine("The hero pats his pockets as if he forgot something at home...");
}
}
然后组成你的英雄类型:
class Program
{
static void Main(string[] args)
{
IHeroAbility swingSwordAbility = new SwingSwordAbility();
IHeroAbility shootArrowAbility = new ShootArrowAbility();
IHeroAbility castFireBallAbility = new CastFireBallAbility();
var fighter = new Hero("The Fighter", new[] { swingSwordAbility });
fighter.Attack();
var mage = new Hero("The Mage", abilities: new[] { castFireBallAbility });
mage.Attack();
var archer = new Hero("The Archer", abilities: new[] { shootArrowAbility });
archer.Attack();
var knight = new Hero("The Knight", abilities: new[] { swingSwordAbility, shootArrowAbility });
knight.Attack();
var paladin = new Hero("The Paladin", abilities: new[] { swingSwordAbility, castFireBallAbility });
paladin.Attack();
var ranger = new Hero("The Ranger",abilities: new[] { shootArrowAbility, castFireBallAbility });
ranger.Attack();
}
}
如果您想要更明确的操作组,您可以将 Hero 类修改为如下内容:
public class Hero
{
private readonly string name;
private readonly IEnumerable<IHeroAbility> meleeAbilities;
private readonly IEnumerable<IHeroAbility> magicAbilities;
private readonly IEnumerable<IHeroAbility> rangedAbilities;
public Hero(
string name,
[Optional] IEnumerable<IHeroAbility> meleeAbilities,
[Optional] IEnumerable<IHeroAbility> magicAbilities,
[Optional] IEnumerable<IHeroAbility> rangedAbilities)
{
this.name = name;
var defaultAbilities = new List<IHeroAbility> { new OhCrap() };
this.meleeAbilities = meleeAbilities ?? defaultAbilities;
this.magicAbilities = magicAbilities ?? defaultAbilities;
this.rangedAbilities = rangedAbilities ?? defaultAbilities;
}
public void MeleeAttack()
{
var ability = meleeAbilities.First();
Console.WriteLine(name);
ability.Execute();
}
public void MagicAttack()
{
var ability = magicAbilities.First();
Console.WriteLine(name);
ability.Execute();
}
public void RangedAttack()
{
var ability = rangedAbilities.First();
Console.WriteLine(name);
ability.Execute();
}
}
然后您可以开始构建它以包含更复杂的逻辑,例如与技能、冷却时间、技能选择等相关的成本等。
希望这会有所帮助!