Difference between revisions of "Encapsulation - 2017"
(2 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
} | } | ||
+ | |||
+ | class Main { | ||
+ | public Main { | ||
+ | int fetchedData = Encapsulation.getData1(); | ||
+ | string fetchedData2 = Encapsulation.getData2(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | The public methods getData1() and getData2() are accessible by anybody and are used to safely get the data stored in private variables, such as data1 and data2. | + | The public methods getData1() and getData2() are accessible by anybody (in any other class) and are used to safely get the data stored in private variables, such as data1 and data2, from the class they're part of. |
+ | |||
+ | From class Animal: | ||
+ | <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> |
Latest revision as of 14:25, 6 February 2017
Encapsulation is the process of combining functions and data into a single place, a class in C# (and others), that prevents the user from directly accessing the data stored. They are stored as private, public & protected. Public can be accessed from any class, private can only be accessed from within the same class and protected can be accessed from the same class or one derived from it.
class Encapsulation {
public getData1();
public getData2();
private int data1;
private string data2;
}
class Main {
public Main {
int fetchedData = Encapsulation.getData1();
string fetchedData2 = Encapsulation.getData2();
}
}
The public methods getData1() and getData2() are accessible by anybody (in any other class) and are used to safely get the data stored in private variables, such as data1 and data2, from the class they're part of.
From class Animal:
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++;
}
...