I am using ActionScript3, I am writing a class that extends Bitmap, I want to have all of Bitmap’s behaviors and I also want to treat to replace its bitmapData with a subclass of bitmapData that offers more flexibility.
So basically what I have is(metaphor):
Class Gunman
{
//Has-A
public var pistol : Gun;
public function Gunman(gun : Gun)
{
this.pistol = gun;
}
//methods
public function shoot():void ..
{
pistol.fire();
}
}
Class Gun
{
//constructor omitted cause it is unnecessary for this example
public function fire():void ..
}
Now I extend these two classes.
Class Automatic extends Pistol
{
override public function fire():void
{
super.fire(); super.fire(); super.fire();
}
}
Class NavySeal extends Gunman
{
public function Enforcer(auto : Automatic)
{
super(auto);
}
}
The problem is that people can still easily do this:
var eliteSoldier : NaveSeal = new Enforce(new Automatic());
eliteSoldier.pistol = new Gun();
And this will be bad. Is there any way I prevent this?
I cannot change ‘Gunman’ cause it is part of the API.
If you are trying to prevent from changing public property
public var pistol:Gun;than you can’t if you haven’t got the control over the class that has defined it. It is a bad design, it should be the getter/setter instead. Then you would be able to get and test what users are passing etc.best regards