class Program
{
static void Main()
{
Character king = new King();
king.Weapon = new KnifeBehavior();
king.Fight();
king.Weapon = new SwordBehavior();
king.Fight();
}
}
public abstract class Character
{
protected IWeaponBehavior? weapon;
public void Fight()
{
weapon?.UseWeapon();
}
public IWeaponBehavior Weapon { set => weapon = value; }
}
public class King : Character
{
}
public interface IWeaponBehavior
{
void UseWeapon();
}
public class KnifeBehavior : IWeaponBehavior
{
public void UseWeapon()
{
Console.WriteLine("ナイフで切る");
}
}
public class SwordBehavior : IWeaponBehavior
{
public void UseWeapon()
{
Console.WriteLine("剣を振りかざす");
}
}
ディスカッション
コメント一覧
まだ、コメントがありません