Difference between revisions of "Instantiation - 2017"
(Created page with "= Instantiation = == Explanation == Instantiation is") |
|||
Line 2: | Line 2: | ||
== Explanation == | == Explanation == | ||
− | Instantiation is | + | Instantiation is creating an instance of a class. In simple terms, it's creating a variable. It is said that the variable is an instance of the class. |
+ | |||
+ | |||
+ | == Skeleton Program Example == | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | |||
+ | //This is the class: | ||
+ | class Simulation | ||
+ | { | ||
+ | private Location[,] Landscape; | ||
+ | private int TimePeriod = 0; | ||
+ | private int WarrenCount = 0; | ||
+ | private int FoxCount = 0; | ||
+ | private bool ShowDetail = false; | ||
+ | private int LandscapeSize; | ||
+ | private int Variability; | ||
+ | private static Random Rnd = new Random(); | ||
+ | |||
+ | public Simulation(int LandscapeSize, int InitialWarrenCount, int InitialFoxCount, int Variability, bool FixedInitialLocations) | ||
+ | { | ||
+ | int menuOption; | ||
+ | int x; | ||
+ | int y; | ||
+ | string viewRabbits; | ||
+ | this.LandscapeSize = LandscapeSize; | ||
+ | this.Variability = Variability; | ||
+ | Landscape = new Location[LandscapeSize, LandscapeSize]; | ||
+ | CreateLandscapeAndAnimals(InitialWarrenCount, InitialFoxCount, FixedInitialLocations); | ||
+ | DrawLandscape(); | ||
+ | do | ||
+ | { | ||
+ | Console.WriteLine(); | ||
+ | Console.WriteLine("1. Advance to next time period showing detail"); | ||
+ | Console.WriteLine("2. Advance to next time period hiding detail"); | ||
+ | Console.WriteLine("3. Inspect fox"); | ||
+ | Console.WriteLine("4. Inspect warren"); | ||
+ | Console.WriteLine("5. Exit"); | ||
+ | Console.WriteLine(); | ||
+ | Console.Write("Select option: "); | ||
+ | menuOption = Convert.ToInt32(Console.ReadLine()); | ||
+ | if (menuOption == 1) | ||
+ | { | ||
+ | TimePeriod++; | ||
+ | ShowDetail = true; | ||
+ | AdvanceTimePeriod(); | ||
+ | } | ||
+ | if (menuOption == 2) | ||
+ | { | ||
+ | TimePeriod++; | ||
+ | ShowDetail = false; | ||
+ | AdvanceTimePeriod(); | ||
+ | } | ||
+ | if (menuOption == 3) | ||
+ | { | ||
+ | x = InputCoordinate('x'); | ||
+ | y = InputCoordinate('y'); | ||
+ | if (Landscape[x, y].Fox != null) | ||
+ | { | ||
+ | Landscape[x, y].Fox.Inspect(); | ||
+ | } | ||
+ | } | ||
+ | if (menuOption == 4) | ||
+ | { | ||
+ | x = InputCoordinate('x'); | ||
+ | y = InputCoordinate('y'); | ||
+ | if (Landscape[x, y].Warren != null) | ||
+ | { | ||
+ | Landscape[x, y].Warren.Inspect(); | ||
+ | Console.Write("View individual rabbits (y/n)?"); | ||
+ | viewRabbits = Console.ReadLine(); | ||
+ | if (viewRabbits == "y") | ||
+ | { | ||
+ | Landscape[x, y].Warren.ListRabbits(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } while (((WarrenCount > 0) || (FoxCount > 0)) && (menuOption != 5)); | ||
+ | Console.ReadKey(); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | //And so an instance can be created from anywhere within a program, for example... | ||
+ | |||
+ | Simulation Sim; | ||
+ | </syntaxhighlight> |
Revision as of 14:01, 6 February 2017
Instantiation
Explanation
Instantiation is creating an instance of a class. In simple terms, it's creating a variable. It is said that the variable is an instance of the class.
Skeleton Program Example
//This is the class:
class Simulation
{
private Location[,] Landscape;
private int TimePeriod = 0;
private int WarrenCount = 0;
private int FoxCount = 0;
private bool ShowDetail = false;
private int LandscapeSize;
private int Variability;
private static Random Rnd = new Random();
public Simulation(int LandscapeSize, int InitialWarrenCount, int InitialFoxCount, int Variability, bool FixedInitialLocations)
{
int menuOption;
int x;
int y;
string viewRabbits;
this.LandscapeSize = LandscapeSize;
this.Variability = Variability;
Landscape = new Location[LandscapeSize, LandscapeSize];
CreateLandscapeAndAnimals(InitialWarrenCount, InitialFoxCount, FixedInitialLocations);
DrawLandscape();
do
{
Console.WriteLine();
Console.WriteLine("1. Advance to next time period showing detail");
Console.WriteLine("2. Advance to next time period hiding detail");
Console.WriteLine("3. Inspect fox");
Console.WriteLine("4. Inspect warren");
Console.WriteLine("5. Exit");
Console.WriteLine();
Console.Write("Select option: ");
menuOption = Convert.ToInt32(Console.ReadLine());
if (menuOption == 1)
{
TimePeriod++;
ShowDetail = true;
AdvanceTimePeriod();
}
if (menuOption == 2)
{
TimePeriod++;
ShowDetail = false;
AdvanceTimePeriod();
}
if (menuOption == 3)
{
x = InputCoordinate('x');
y = InputCoordinate('y');
if (Landscape[x, y].Fox != null)
{
Landscape[x, y].Fox.Inspect();
}
}
if (menuOption == 4)
{
x = InputCoordinate('x');
y = InputCoordinate('y');
if (Landscape[x, y].Warren != null)
{
Landscape[x, y].Warren.Inspect();
Console.Write("View individual rabbits (y/n)?");
viewRabbits = Console.ReadLine();
if (viewRabbits == "y")
{
Landscape[x, y].Warren.ListRabbits();
}
}
}
} while (((WarrenCount > 0) || (FoxCount > 0)) && (menuOption != 5));
Console.ReadKey();
}
//And so an instance can be created from anywhere within a program, for example...
Simulation Sim;