AS3 保护被破坏了?

发布于 2024-12-01 06:52:48 字数 728 浏览 1 评论 0原文

我有一个令人高兴的 Flash AS3 难题。给出以下代码:

public class BaseClass
{
  protected var _A:uint;
}

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

所以你看,静态函数正在利用其读取 BaseClass 的受保护成员的能力,以返回 _A。

但这并不能计算,Flash 对我咆哮道:

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

在 Java 中,这没关系。我认为 Java 是基本 OO 的参考实现。

如果我从 readBaseA 方法中去掉“静态”,那么现在它是一个实例方法,我仍然无法从 BaseClass 实例中读取 _A 。但有趣的是,我可以从这里读到它。_A。

就像 AS3 区分 this 的受保护成员和其他对象的受保护成员一样,尽管这些对象可能是与“this”同一类的实例。

有人在野外见过类似的行为吗?

富有的

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2024-12-08 06:52:48

_A 受到保护,因此只能从同一个类或其子类中访问它,并且由于它没有声明为静态“类”,因此在这方面实际上意味着“类实例”。

澄清一下,实例 a 无法访问实例 b 的私有或受保护属性,无论它们是否具有完全相同的类型或者是否扩展了另一个。

_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.

眼角的笑意。 2024-12-08 06:52:48

如果您使用 internal 而不是 protected 实例属性,它就会起作用。 (如果这对您来说足够隐私的话)

ActionScript 对于 Adob​​e 的 protected 的含义非常特殊:

受保护 - 对同一类和派生类中的引用可见。”

因此,它仅适用于专门的引用,不幸的是,在您的情况下,它们显然仅意味着实例引用。这可能与 ActionScript 所基于的基于原型的实现有关,其中它仅检查 prototype 对象而不是 constructor 对象来确定是否允许访问受保护的成员。

it'll work if you use internal instead of protected instance property. (if that is enough privacy for you)

ActionScript is very particular about what protected means, from Adobe:

"protected - Visible to references in the same class and derived classes."

So its only available to references specifically, and unfortunately in your case, they obviously mean instance references only. This might have something to do with the prototype-based implementation that ActionScript is built on, where its only checking the prototype object and not the constructor object to determine if access is allowed protected member.

青衫儰鉨ミ守葔 2024-12-08 06:52:48

这就是它在 AS3 中的工作方式:

public class BaseClass
{
  protected var _A:uint;
}

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

可以从扩展类访问和覆盖 protected 字段,并且在任何其他情况下都将被视为 private

that's how it works in AS3:

public class BaseClass
{
  protected var _A:uint;
}

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

protected fields can be accessed and overriden from extended classes and are treated as private for any other case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文