Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 protected is clobbered?

I have a joyful Flash AS3 conundrum. Given the following code:

public class BaseClass
{
  protected var _A:uint;
}

public class ExtendedClass extends BaseClass
{
  public static function readBaseA(a:BaseClass) : uint
  {
    return a._A;
  }
}

So you see, the static function is using its ability to read the protected member of BaseClass, to return _A.

But this does not compute, and Flash barks back at me with:

(hidden)\ExtendedClass.as, Line 7   1178: Attempted access of inaccessible property _A through a reference with static type BaseClass.

In Java, this is okay. And I consider Java the reference implementation of basic OO.

if I take away the 'static' from that readBaseA method, so that now it is an instance method, I still cannot read the _A from a BaseClass instance. But interestingly, I can read it from this._A.

It's like AS3 is differentiating between this's protected members, and other objects' protected members, although those objects may be instances of the same class as 'this'.

Anyone seen similar behaviour out there in the wild?

Rich

like image 733
4 revs, 4 users 79% Avatar asked Feb 23 '23 04:02

4 revs, 4 users 79%


1 Answers

_A is protected so it can only be accessed from within the same class or it's sub-classes, and since it's not declared as static "class" in this regard actually means "class instance".

To clarify, an instance a cannot access a private or protected property of instance b, regardless of whether they have the exact same type or if one extends the other.

like image 62
richardolsson Avatar answered Mar 16 '23 22:03

richardolsson