interface IBigWeapon
{
void Chop();
void Wheel();
}
interface ISmallWeapon
{
void Brandish();
void Pierce();
}
class Axe : IBigWeapon
{
#region BigWeapon 成员
public void Chop()
{
Console.Write("落斧劈砍,金钢可破!");
}
public void Wheel()
{
Console.Write("轮斧生风,势不可挡!");
}
#endregion
}
class Knife : ISmallWeapon
{
#region ISmallWeapon 成员
public void Brandish()
{
Console.Write("挥动匕首,扰敌心智! ");
}
public void Pierce()
{
Console.Write("匕首刺杀,以快致胜! ");
}
#endregion
}
class Sword : ISmallWeapon
{
#region ISmallWeapon 成员
public void Brandish()
{
Console.Write("随风舞剑,可防可攻!");
}
public void Pierce()
{
Console.Write("借风刺剑,无人可挡!");
}
#endregion
}
class Linghuchong
{
public void Attack1(ISmallWeapon weapon)
{
Console.WriteLine("");
Console.Write("独孤九剑第一式:");
weapon.Brandish();
weapon.Pierce();
}
public void Attack2(ISmallWeapon weapon)
{
Console.WriteLine("");
Console.Write("独孤九剑第二式:");
weapon.Pierce();
weapon.Brandish();
}
}
class BigWeaponAdapter : ISmallWeapon
{
private IBigWeapon BigWeapon;
#region ISmallWeapon 成员
public void Brandish()
{
this.BigWeapon.Wheel();
}
public void Pierce()
{
this.BigWeapon.Chop();
}
#endregion
public BigWeaponAdapter(IBigWeapon weapon)
{
this.BigWeapon = weapon;
}
}
Linghuchong lhc = new Linghuchong();
IBigWeapon axe = new Axe();
ISmallWeapon knife = new Knife();
ISmallWeapon sword = new Sword();
ISmallWeapon axeAdapter = new BigWeaponAdapter(axe);
//lhc.Attack1(axe);//产生异常
//用适配器
lhc.Attack1(axeAdapter);
lhc.Attack1(knife);
lhc.Attack1(sword);
发表于 @ 2008年06月26日 17:04:00 | 评论( loading... ) | 举报| 收藏