2021 - Base

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

What is Base?

From within a SubClass, 'Base' will refer to the parent 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;
        }

    }

The constructor for PBDSPiece includes ':base(player1)' this will pass the parameter from the PBDSPiece constructor to the 'base' constructor for 'Piece'.

'base' could also be used to run a method within the parent class, so for example:

        public override int CheckMoveIsValid(int distanceBetweenTiles, string startTerrain, string endTerrain)
        {
            if (distanceBetweenTiles != 1 || startTerrain == "~")
            {
                return -1;
            }
            return fuelCostOfMove;
        }

This code will 'override' the 'CheckMoveIsValid' method of the parent class. If the code allowed, we could run the parent method as well using:

        public override int CheckMoveIsValid(int distanceBetweenTiles, string startTerrain, string endTerrain)
        {
            base.CheckMoveIsValid(int distanceBetweenTiles, string startTerrain, string endTerrain);
        }