Difference between revisions of "Inheritance - 2017"
(Created page with "Inheritance is where an object is based on another object, gaining all off the classes behaviors for example if "Lewis" was a subclass of "human" it would gain all of the move...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | Inheritance is where an object is based on another object, gaining all off the classes behaviors for example if "Lewis" was a subclass of "human" it would gain all of the movement attributes, food needed, 2 arms, 2 legs etc. But it can also gain other attributes that aren't in human while maintaining all the current ones. | + | Inheritance is where an object is based on another object, gaining all off the classes behaviors for example if "Lewis" was a subclass of "human" it would gain all of the movement attributes, food needed, 2 arms, 2 legs etc. But it can also gain other attributes that aren't in human while maintaining all the current ones. <br> |
+ | Example in the skeleton program <br> | ||
+ | Main Class | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | class Animal | ||
+ | { | ||
+ | protected double NaturalLifespan; | ||
+ | protected int ID; | ||
+ | protected static int NextID = 1; | ||
+ | protected int Age = 0; | ||
+ | protected double ProbabilityOfDeathOtherCauses; | ||
+ | protected bool IsAlive; | ||
+ | protected static Random Rnd = new Random(); | ||
+ | |||
+ | public Animal(int AvgLifespan, double AvgProbabilityOfDeathOtherCauses, int Variability) | ||
+ | { | ||
+ | NaturalLifespan = AvgLifespan * CalculateRandomValue(100, Variability) / 100; | ||
+ | ProbabilityOfDeathOtherCauses = AvgProbabilityOfDeathOtherCauses * CalculateRandomValue(100, Variability) / 100; | ||
+ | IsAlive = true; | ||
+ | ID = NextID; | ||
+ | NextID++; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | Subclass with inherited values | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | class Fox : Animal | ||
+ | { | ||
+ | private int FoodUnitsNeeded = 10; | ||
+ | private int FoodUnitsConsumedThisPeriod = 0; | ||
+ | private const int DefaultLifespan = 7; | ||
+ | private const double DefaultProbabilityDeathOtherCauses = 0.1; | ||
+ | |||
+ | public Fox(int Variability) | ||
+ | : base(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability) | ||
+ | { | ||
+ | FoodUnitsNeeded = (int)(10 * base.CalculateRandomValue(100, Variability) / 100); | ||
+ | } | ||
+ | </syntaxhighlight> |
Latest revision as of 14:50, 6 February 2017
Inheritance is where an object is based on another object, gaining all off the classes behaviors for example if "Lewis" was a subclass of "human" it would gain all of the movement attributes, food needed, 2 arms, 2 legs etc. But it can also gain other attributes that aren't in human while maintaining all the current ones.
Example in the skeleton program
Main Class
class Animal
{
protected double NaturalLifespan;
protected int ID;
protected static int NextID = 1;
protected int Age = 0;
protected double ProbabilityOfDeathOtherCauses;
protected bool IsAlive;
protected static Random Rnd = new Random();
public Animal(int AvgLifespan, double AvgProbabilityOfDeathOtherCauses, int Variability)
{
NaturalLifespan = AvgLifespan * CalculateRandomValue(100, Variability) / 100;
ProbabilityOfDeathOtherCauses = AvgProbabilityOfDeathOtherCauses * CalculateRandomValue(100, Variability) / 100;
IsAlive = true;
ID = NextID;
NextID++;
}
Subclass with inherited values
class Fox : Animal
{
private int FoodUnitsNeeded = 10;
private int FoodUnitsConsumedThisPeriod = 0;
private const int DefaultLifespan = 7;
private const double DefaultProbabilityDeathOtherCauses = 0.1;
public Fox(int Variability)
: base(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability)
{
FoodUnitsNeeded = (int)(10 * base.CalculateRandomValue(100, Variability) / 100);
}