I have abstract class Ship and 2 other classes are inherited from it Battleship and Cruiser.
public abstract class Ship
{
public Vector2Int Position;
public int Size;
public abstract void Move();
public abstract void Attack();
}
public class Cruiser : Ship
{
public override void Attack()
{
Debug.Log("ATTACK");
}
public override void Move()
{
Debug.Log("Moves forward at a speed of 2 cell per step");
}
}
public class Battleship : Ship
{
public override void Attack()
{
Debug.Log("ATTACK");
}
public override void Move()
{
Debug.Log("Moves forward at a speed of 1 cell per step");
}
}
Is it possible to get a reference to a child class from an abstract one?
public class Main
{
private Ship[] _ships = new Ship[] { new Battleship(), new Battleship(), new Cruiser() };
public void StepUpdate()
{
for (int i = 0; i < _ships.Length; i++)
{
_ships[i].Move();
_ships[i].Attack();
//_ships[i] is Battleship or Cruiser !?
//Battleship bs = _ships[i] ?
}
}
}