I'm stuck in this situation where:
- I have an abstract class called
Ammo
, with AmmoBox
and Clip
as children.
- I have an abstract class called
Weapon
, with Firearm
and Melee
as children.
Firearm
is abstract, with ClipWeapon
and ShellWeapon
as children.
- Inside
Firearm
, there's a void Reload(Ammo ammo);
The problem is that, a ClipWeapon
could use both a Clip
and an AmmoBox
to reload:
public override void Reload(Ammo ammo)
{
if (ammo is Clip)
{
SwapClips(ammo as Clip);
}
else if (ammo is AmmoBox)
{
var ammoBox = ammo as AmmoBox;
// AddBullets returns how many bullets has left from its parameter
ammoBox.Set(clip.AddBullets(ammoBox.nBullets));
}
}
But a ShellWeapon
, could only use an AmmoBox
to reload. I could do this:
public override void Reload(Ammo ammo)
{
if (ammo is AmmoBox)
{
// reload...
}
}
But this is bad because, even though I'm checking to make sure it's of type AmmoBox
, from the outside, it appears like a ShellWeapon
could take a Clip
as well, since a Clip
is Ammo
as well.
Or, I could remove Reload
from Firearm
, and put it both ClipWeapon
and ShellWeapon
with the specific params I need, but doing so I will lose the benefits of Polymorphism, which is not what I want to.
Wouldn't it be optimal, if I could override Reload
inside ShellWeapon
like this:
public override void Reload(AmmoBox ammoBox)
{
// reload ...
}
Of course I tried it, and it didn't work, I got an error saying the signature must match or something, but shouldn't this be valid 'logically'? since AmmoBox
is a Ammo
?
How should I get around this? And in general, is my design correct?
(Note I was using interfaces IClipWeapon
and IShellWeapon
but I ran into trouble, so I moved to using classes instead)
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…