AS3 Only allow access to certain properties on DisplayObject
I have a getter within a class which is used to make my property skin:DisplayObject
read-only. This class also has a property body:AvBody
which is used to define the x
y
width
height
etc of the object, which in turn positions the skin
.
What I want to do is disable access to the properties x
and y
on skin
unless accessed 开发者_StackOverflow社区from within the internal
namespace (where AvBody
is also located) Is this possible?
Note: I can't make skin any type other than DisplayObject
(I can't even make skin
a class that extends DisplayObject
because it creates issues with library symbols).
Since you can't have an individual skin class just put skin into a namespace that only AvBody and AvChild can access.
i believe you're running into a design issue here, and you would probably be able to avoid wanting or needing to do this with some restructuring of code. perhaps you could merge skin
and avBody
into one class? you can decide and for now let's assume that a redesign is not the solution.
unfortunately, since function overloading is not possible in AS3, a solution like the following is also not possible since overrides must have identical signatures:
override public function set x(value:Number, caller:*):void
{
if (caller is AvBody)
super.x = value;
}
//Error: Incompatible override.
you could simply override x
and y
setters, displaying an ArgumentError
if they are called, and instead use custom internal setters for the x
and y
properties, even though it's kinda ghetto.
override public function set x(value:Number):void
{
throw new ArgumentError("Can not set public 'x' property on Skin class. Set internal 'posX' property instead");
}
override public function set y(value:Number):void
{
throw new ArgumentError("Can not set public 'y' property on Skin class. Set internal 'posY' property instead");
}
internal function set posX(value:Number):void
{
super.x = value;
}
internal function set posY(value:Number):void
{
super.y = value;
}
No, as far as I know you can't do that. But as TheDarkIn198 mentioned, you have a design issue that would be good to look at. Instead of merging body and skin, I would suggest implement the body (AvBody) features on the actual class. Or make a new class, called Body or something, that every object that have body will inherit from.
But as I know that you are developing a framework, you don't need to limit its use, since the framework will be internal and the user of the framework won't need to have access to skin, only for exceptional cases, so you can disable it completely via a namespace. Here I'm hoping you have methods that serves every need that the user may have, for example adding an object to another (inheritance, similar to addChild). You will be amazed to know that some libraries work. TweenMax, for example, makes every internal variable public, but in the comment they put /** @private **/
.
So, in my opinion, its best to not limit the variable, but to have it well documented.
精彩评论