June 2018
Intermediate to advanced
310 pages
6h 32m
English
Squad, clearly, must be a collection of infantry units. So, it should be easy:
class Squad(val infantryUnits: MutableList<InfantryUnit> = mutableListOf())
We even set up a default parameter value, so the other programmer won't need to pass his own list of soldiers unless he really needs too. MutableList suits us well here, since we may add units later.
To make sure it works, we'll create three soldiers and put them inside:
val miller = Rifleman()val caparzo = Rifleman()val jackson = Sniper()val squad = Squad()squad.infantryUnits.add(miller)squad.infantryUnits.add(caparzo)squad.infantryUnits.add(jackson)println(squad.infantryUnits.size) // Prints 3
But the next day, Dave, that's the other programmer, comes to us with a new requirement. ...
Read now
Unlock full access