samedi 16 mai 2020

Is there a way to accept unknown types in runtime?

I am trying to solve a simple OOPs problem. When I have to create few weapons and each weapon has a primary action and that primary action can be performed by a mouse click. Example for a shotgun it is shooting and for Katana is swinging it. Below my classes are.

public interface IShootable
{
    void TakeShot();
}
public interface ISwingable
{
    void Swing ();
}

public class ShotGun : IShootable
{
    public void TakeShot()
    {
    }
}
public class Kanata : ISwingable
{
    public void Swing ()
    {
    }
}   

Each weapon has implemented different interface for their primary actions. (I'm not sure that I can create an abstract class, from which I can inherit these concrete classes. It seems not substitutable for two different type of weapons.)

What I wanted to achieve is in runtime when user selects one of the weapons, the user gets the right action on mouse click. For shotgun it is TakeShot() and for Katana it is Swing().

What I have to do adopt that. Or I should restructure the classes in some other way.

Aucun commentaire:

Enregistrer un commentaire