使用 Ninject 重写以下示例会是什么样子?
具体来说,您如何将武士绑定到手里剑和剑上?
(来自https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand)
interface IWeapon
{
void Hit(string target);
}
class Sword : IWeapon
{
public void Hit(string target)
{
Console.WriteLine("Chopped {0} clean in half", target);
}
}
class Shuriken : IWeapon
{
public void Hit(string target)
{
Console.WriteLine("Pierced {0}'s armor", target);
}
}
class Program
{
public static void Main()
{
var warrior1 = new Samurai(new Shuriken());
var warrior2 = new Samurai(new Sword());
warrior1.Attack("the evildoers");
warrior2.Attack("the evildoers");
/* Output...
* Piereced the evildoers armor.
* Chopped the evildoers clean in half.
*/
}
}