lordofduct
05-04-2008, 11:39 PM
I want to be able to have a property of an Class that is protected, but can be pulled with a get function. BUT this get function protects the props of this object.
for instance:
public class CustomClass
{
protected var _bounds:Rectangle;
public function get bounds():Rectangle { return _bounds; }
public function CustomClass(x:Number, y:Number, w:Number, h:Number):void
{
_bounds = new Rectangle(x,y,w,h);
}
}
but if I instantiate it and say:
myCustomClass.bounds.width = 500;
the bounds will be updated. How can I make it where the props of this protected object stay protected?
I know I could pass a clone of the property which would keep it protected, but takes up more memory. OR I could set functions in the CustomClass that only return these sub props, and doesn't set them. Kinda like this:
public class CustomClass
{
protected var _bounds:Rectangle;
public function get boundsWidth():Number { return _bounds.width; }
public function get boundsHeight():Number { return _bounds.height; }
public function CustomClass(x:Number, y:Number, w:Number, h:Number):void
{
_bounds = new Rectangle(0,0,200,200);
}
}
but that's a bit of an annoyance, and it doesn't feel as natural to have to say:
var h:Number = myCustomClass.boundsHeight;
as it is to say:
var h:Number = myCustomClass.bounds.height;
That in this latter choice makes it REALLY annoying to use methods supplied by these protected objects. Like hitTests or the sort.
any suggestions?
for instance:
public class CustomClass
{
protected var _bounds:Rectangle;
public function get bounds():Rectangle { return _bounds; }
public function CustomClass(x:Number, y:Number, w:Number, h:Number):void
{
_bounds = new Rectangle(x,y,w,h);
}
}
but if I instantiate it and say:
myCustomClass.bounds.width = 500;
the bounds will be updated. How can I make it where the props of this protected object stay protected?
I know I could pass a clone of the property which would keep it protected, but takes up more memory. OR I could set functions in the CustomClass that only return these sub props, and doesn't set them. Kinda like this:
public class CustomClass
{
protected var _bounds:Rectangle;
public function get boundsWidth():Number { return _bounds.width; }
public function get boundsHeight():Number { return _bounds.height; }
public function CustomClass(x:Number, y:Number, w:Number, h:Number):void
{
_bounds = new Rectangle(0,0,200,200);
}
}
but that's a bit of an annoyance, and it doesn't feel as natural to have to say:
var h:Number = myCustomClass.boundsHeight;
as it is to say:
var h:Number = myCustomClass.bounds.height;
That in this latter choice makes it REALLY annoying to use methods supplied by these protected objects. Like hitTests or the sort.
any suggestions?