Arcadian
Arcadian

Reputation: 1373

Inheritance solution

I've been working on building several classes which inherit from one base class but I'm not entirely confident on how inheritance and polymorphism work in C# at this stage.

My base class looks like this:

abstract class Structure
    {
        public int currentCost = 0;
        public int currentArea = 0;
        public int currentPopulation = 0;
        public int currentConstruction = 0;
        public int currentEnergy = 0;
        public int currentEconomy = 0;

        public abstract int baseCost { get; }
        public abstract int baseEnergy { get; }
        public abstract int baseEconomy { get; }
        public abstract int baseConstruction { get; }
        public int baseArea = -1;
        public int basePopulation = -1;

        public int level = 0;
        public abstract string structureName { get; }
}

Now, classes that inherit from the Structure class will be made to provide their own assignments for the abstract variables which is fine as most of the classes vary wildly in the figures they assign. The abstract variables are used in the derived classes in the following (incomplete) manner:

class BiosphereModification : Structure
    {
        const int baseEconomyBiosphereModification = 0;
        const int baseConstructionBiosphereModification = 0;
        const int baseCostBiosphereModification = 2000;
        const int baseEnergyBiosphereModification = 0;
        const int baseFertilityBiosphereModification = 1;
        const string structureNameBiosphereModification = "BiosphereModification";

        public override int baseCost { get { return baseCostBiosphereModification; } }
        public override int baseEconomy { get { return baseEconomyBiosphereModification; } }
        public override int baseEnergy { get { return baseEnergyBiosphereModification; } }
        public override int baseConstruction { get { return baseConstructionBiosphereModification; } }
}

However, the non-abstract variables will be the same across the majority of derived classes, but not all of them.

I could make them all abstract and force each class to provide it's own value, but this seems counter-intuitive. What I would prefer is a way to provide a value in the base class and provide an override in a derived class if needed.

Is there a way to do this? I know that this can be done with methods declared virtual. This allows the derived class to use the base classes method unless it provides one of it's own. Surely a similar thing exists for this?

Upvotes: 2

Views: 258

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564413

What I would prefer is a way to provide a value in the base class and provide an override in a derived class if needed.

Properties can be declared virtual, as well:

public virtual int BaseCost { get { return 0; } }
public virtual int BaseEnergy { get { return 42; } }
public virtual int BaseEconomy { get { return 3982; } }
public virtual int BaseConstruction { get { return 398829; } }

You can then override them when appropriate:

public override int BaseCost { get { return 2; } }

Upvotes: 3

Related Questions