Difference between revisions of "2021 - Static"
(Created page with "=What is Static?= Within OOP programming, Static is a method which can only be called without creating an object of the class. =Where is it used?= This can be seen in the 'PB...") |
(→What is Static?) |
||
Line 1: | Line 1: | ||
=What is Static?= | =What is Static?= | ||
− | Within OOP programming, Static is a method which can | + | Within OOP programming, Static is a method which can be called without creating an object of the class. |
=Where is it used?= | =Where is it used?= |
Revision as of 10:22, 24 September 2020
What is Static?
Within OOP programming, Static is a method which can be called without creating an object of the class.
Where is it used?
This can be seen in the 'PBDSPiece' class:
class PBDSPiece : Piece
{
static Random rNoGen = new Random();
public PBDSPiece(bool player1)
: base(player1)
{
pieceType = "P";
VPValue = 2;
fuelCostOfMove = 2;
}
public override int CheckMoveIsValid(int distanceBetweenTiles, string startTerrain, string endTerrain)
{
if (distanceBetweenTiles != 1 || startTerrain == "~")
{
return -1;
}
return fuelCostOfMove;
}
public int Dig(string terrain)
{
if (terrain != "~")
{
return 0;
}
if (rNoGen.NextDouble() < 0.9)
{
return 1;
}
else
{
return 5;
}
}
}
'rNoGen' is declared as static.
How is it used?
If we created a new 'PBDSPiece':
<syntaxhighlight lang=c#>
PBDSPiece piece = new PBDSPiece(true);
</syntaxhiglight>
You would not be able to use: <syntaxhighlight lang=c#>
piece.rNoGen;
</syntaxhiglight>
Static methods are not available within the objects of a class.