简体   繁体   中英

Is there any way to use different constructors depending on initial enum value?

I am currently working on developing a dungeon-crawler-esque game in c# but am currently having trouble with the constructors of a class. I was wondering if it is possible to have different constructors depending on the initial enum value? In this case, it only has two possible values so a simple boolean would suffice but if I was to have more than two, I was curious if you could execute different constructors depending on the initial enum value entered? Sorry if my formatting is wrong or if I'm not very specific, this is my first time submitting a question on here :)).

For reference here are my current enums here:

public enum Type
{
    Passive,
    Action
}

public enum Trigger
{
    PlayerTakeDamage,
    EnemyTakeDamage,
    Draw,
    PlayerDebuff,
    EnemyDebuff,
    PlayCard
}

Here is along the lines of what I want to accomplish:

public class Action
{
    public int Energy {get; private set;}
    public int Damage {get; private set;}

    public Action(Type.Passive, Trigger trigger, int damage, int energy)
    {
        do thing;
    }
    public Action(Type.Action, int damage, int energy)
    {
        do other thing;
    }
}

Again, I apologize if this is a dumb question or if I messed up the formatting on here, but I would sincerely appreciate any advice or recommendations that anyone could offer :))!

Edit: I'm sorry I wasn't very specific about the potential applications. I want to overload the constructors based on the intial enum value because I want to have completed different additional parameters after that would be required upon construction. I've added clarification to help :)).

We can have constructor overloading with different signature. The constructor overloading is done by number of parameters, different type of parameters but not by the value. I have simplified your code by using condition, Hope this may help.

public class Action
{
    public Action(Type t)
    {  
        if(t == Type.Passive)
        { 
            //do thing; 
        }
        else if(t== Type.Action)
        { 
            //do another thing; 
        }
    }
}

Something like this might be what you are looking for in concept.

public class Person
{
    private int _Speed;
    private double _ReactionTime;
    private int _CalorieConsumptionPerHour;

    public Person(Stance stance) 
    {
        if (stance == Stance.Active)
            setToActive();
        else if (stance == Stance.Passive)
            setToPassive();
        else
            setToDefault();
    }

    private void setToActive()
    {
        _Speed = 30;
        _ReactionTime = .15;
        _CalorieConsumptionPerHour = 200;
    }

    private void setToPassive()
    {
        _Speed = 10;
        _ReactionTime = .5;
        _CalorieConsumptionPerHour = 100;
    }

    private void setToDefault()
    {
        _Speed = 20;
        _ReactionTime = .3;
        _CalorieConsumptionPerHour = 150;
    }

    public enum Stance
    {
        Active,
        Passive
    }
}

As mentioned by both answers, you can pass the enum to the constructor, and create a new object based on said enum.

I would take a differenct approach altogether. Ever heard of IoC (Inversion Of Control) What is Inversion of Control?

The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.

What does that mean in your case?

Lets assume that there are different type of actions, all adhering to the same rules (same methods).

This screams Interface in OOP languages

public Interface IAction {
    void DoSomething();
    int DoSomethingElse();
}

Now, all actions have some standard things they do and diff in others

public abstract AbstractAction: IAction {
    public int Energy {get; private set;}
    public int Damage {get; private set;}
}

Now create different classes for each action

public class PlayerTakeDamageAction: AbstractAction {

    public PlayerTakeDamageAction(/*some parameters*/) {
    }

    // Override the behavior per class
}

What does all this have to do with you? Now you can apply IoC by using Dependency Injection or the FactoryPattern. That means that you delegate the creation of the IAction object to someone else.

Here is a trivial example of an abstract factory

public class ActionConsumer {
    private IActionFactory factory;

    public A(IActionFactory factory) {
        this.factory = factory;
    }

    public void DoSomething() {
        IAction a = factory.MakeAction(Trigger.PlayerTakeDamageAction);
        a.DoSomething();
    }
}

public ConcreteActionFactory: IActionFactory {
    IAction MakeAction() {
        return new PlayerTakeDamageAction();
    }

    IAction MakeAction(Trigger trigger) {
        if (Trigger == Trigger.PlayerTakeDamageAction)
           return new PlayerTakeDamageAction();
        else if // you get the idea
    }
}

interface IActionFactory {
    // Creates some default action
    IAction MakeAction();
    // Creates some specific action
    // You can pass any parameters or create multiple action
    // generators, depending on what the new object needs.
    IAction MakeAction(Trigger trigger);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM