Abstract - Virtual - Static - 2017

From TRCCompSci - AQA Computer Science
Revision as of 06:41, 26 May 2017 by Admin (talk | contribs) (Created page with "==Virtual== If a value is declared as virtual, a sub class can override the value. ===For Example=== The inspect method within the Animal class: <syntaxhighlight lang=csharp...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Virtual

If a value is declared as virtual, a sub class can override the value.

For Example

The inspect method within the Animal class:

    public virtual void Inspect()
    {
      Console.Write("  ID " + ID + " ");
      Console.Write("Age " + Age + " ");
      Console.Write("LS " + NaturalLifespan + " ");
      Console.Write("Pr dth " + Math.Round(ProbabilityOfDeathOtherCauses, 2) + " ");
    }

The fox class overrides this:

    public override void Inspect()
    {
      base.Inspect();
      Console.Write("Food needed " + FoodUnitsNeeded + " ");
      Console.Write("Food eaten " + FoodUnitsConsumedThisPeriod + " ");
      Console.Write("Gender " + Gender + " ");
      Console.WriteLine();
    }

So does the rabbit class:

    public override void Inspect()
    {
      base.Inspect();
      Console.Write("Rep rate " + Math.Round(ReproductionRate, 1) + " ");
      Console.WriteLine("Gender " + Gender + " ");
    }