I know the title’s a bit wordy, but I don’t know how else to ask this question. This is basically the technique that I’ve been using to filter the types of objects you pass into an inherited class. Have a look at the code first and I’ll explain more…
public interface IProjectile {}
public interface IPaintBall : IProjectile {}
public interface IPotato : IProjectile {}
public class Prop
{
public void Shoot(params IProjectile[] projectiles)
{
// logic goes here...
}
}
public class Car : Prop
{
public override void Shoot(params IPaintBalls[] paintBalls)
{
base.Shoot(paintBalls);
}
}
See? I don’t want you to shoot my car with potatoes. You can only shoot it with paint-balls. So am I doing this the right way? Again, this gets way more complicated when the Prop class has like 100 functions that I also want to filter down to just paint-balls. I don’t want to write-out those 100+ functions for the Car class, right? I especially don’t want to write-out those 100+ functions for the 100+ Car classes that I’ll be writing.
Am I being clear enough here?
This is just an example. I’m not doing game programming or anything like that. I’m just trying to give you guys a really simple example to convey what I want here. Basically, I don’t want the code to compile if someone is trying to pass a potato to the Car’s Shoot() function.
What I would probably do is to make Prop accept a Generic Type. I.e:
The methods are then exposed as PaintBalls for users of the Car class, i.e:
You can then also have a non generic Prop class:
Where you can still use:
Personcan be shot with anyIProjectile, includingIPotato.