TypeScript Access Static Variables Using Instances

So in most OOP languages static variables can also be called class variables, ie their value is shared among all instances of this class. For example, in my game I have a class Bullet which is extended by GreenBullet and PinkBullet. I want these subclasses to have a “class” or “static” variable called ammo so that I can keep track of the ammo count for that specific ammo type. But here is the catch: I want to be able to access this property through an instance of the subclass.

Example:

var bullet: GreenBullet = new GreenBullet()
if (bullet.ammo <= 0)
    return;
bullet.shoot();
bullet.ammo --;

I want ALL instances of GreenBullet to be aware of this change to their ammo count.


Source: oop

Leave a Reply